Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
Writing data to Firebase using Vue

Please help. I've been trying for days, watched countless tutorials, and tried different combination of methods on Firebase documentations, but nothing seemed to work.

I'm building a Vue chat project and trying to write data into firebase databases. The code in my firebase/init.js is as follows:

import { initializeApp } from "firebase/app";
import { getFirestore} from 'firebase/firestore';

const firebaseConfig = {
  apiKey: "XXXXXXXXXXXXXXXXXXXXXXXXXX",
  authDomain: "XXXXXXXXXXXXXXXXXXXXXXXXXX",
  projectId: "XXXXXXXXXXXXXXXXXXXXXXXXXX",
  storageBucket: "XXXXXXXXXXXXXXXXXXXXXXXXXX",
  messagingSenderId: "XXXXXXXXXXXXXXXXXXXXXXXXXX",
  appId: "XXXXXXXXXXXXXXXXXXXXXXXXXX",
  measurementId: "XXXXXXXXXXXXXXXXXXXXXXXXXX"
};


const app = initializeApp(firebaseConfig);

const db = getFirestore(app);

export default db;

The code in my CreateMessage.vue file is as follows:

<script>
import {collection, addDoc} from "firebase/firestore";
export default {
    name: 'CreateMessage',
    props: ['name'],
    data() {
        return {
            newMessage: null,
            errorText: null
        }
    },
    methods: {
        createMessage () {
            if (this.newMessage != "") {
                addDoc(collection(firestore, "messages"),{
                    message: this.newMessage,
                    name: this.name,
                    timestamp: Date.now()
                });
                this.newMessage = null;
                this.errorText = null;
            } else {
                this.errorText = "A message must be entered first!";
            }
        }
    }    
}
</script>

Any help would be greatly appreciated. Thank you all very much in advance.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The only thing you are exporting from your init.js file is db. And you never import db into your component. Should import like this

import { db } from 'firebase/init.js

and then add the doc...

addDoc(collection(db, 'messages'), ...

Alternatively, you don't have to import db. You could just also import getFirestore from firebase/firestore and just do...

addDoc(collection(getFirestore(), 'messages'), ...
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda