Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!