Home > Back-end >  How to send data from my code to firestore?
How to send data from my code to firestore?

Time:06-27

I am trying to add an array to my firestore database, but I can't seem to do so as it keeps giving me an error that ColRef.add is not a function. Help! Below shows two of my code snippets. One for firebase.js while the other is where I intend to send my data to firestore from. The array has no issues.

 onAuthStateChanged(auth, (user) => {
      if (user) {
        setUser(user.uid);
      } else {
        console.log("no user found")
      }
    })

    const colRef = collection(db, 'user_data');

    useEffect(() => {
      setSend([{
        averagepace: calculatePace(distance, seconds),
        distance: distance.toFixed(2),
        time: showTime(seconds),
        day: getDayname(),
        uid: user,
        picture:'https://media.wired.com/photos/59269cd37034dc5f91bec0f1/191:100/w_1280,c_limit/GoogleMapTA.jpg',
      }])
      console.log(send);
    }, [end]);

    const res = colRef.add(send);

// import all functions needed from the SDKS needed
import {initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getFirestore } from "firebase/firestore";
import { Firestore, getDoc, collection, getDocs,
  addDoc, deleteDoc, doc,
  query, where, onSnapshot

} from 'firebase/firestore';


const FIREBASE_APIKEY="AIzaSyD1Hk1asVux06fU55J3FaaYfyIUI4sM1B4"
const FIREBASE_AUTHDOMAIN="stressgo-3cef0.firebaseapp.com"
const FIREBASE_PROJECTID="stressgo-3cef0"
const FIREBASE_STORAGEBUCKET="stressgo-3cef0.appspot.com"
const FIREBASE_MESSAGINGSENDERID="316778582160"
const FIREBASE_APPID="1:316778582160:web:3502f9529218e8fc838815"
const FIREBASE_MEASUREMENTID="G-BLT8YK6SMG"

// Initialize Firebase
const firebaseConfig = {
    apiKey: FIREBASE_APIKEY,
    authDomain: FIREBASE_AUTHDOMAIN,
    projectId: FIREBASE_PROJECTID,
    storageBucket: FIREBASE_STORAGEBUCKET,
    messagingSenderId: FIREBASE_MESSAGINGSENDERID,
    appId: FIREBASE_APPID,
    measurementId: FIREBASE_MEASUREMENTID,
  }

  const app = initializeApp(firebaseConfig)
  export const auth = getAuth(app);
  export const db = getFirestore(app);
  

CodePudding user response:

You're trying to use Firestore SDK v8 syntax instead of v9, which is what you have installed. Please refer to the v9 samples in the documentation and use the addDoc function instead.

addDoc(colRef, send)
  • Related