0

I am trying to make a collection as seen in the code below. But trying to do it is giving me the error:

Unhandled Rejection (TypeError): _firebase_firebase_config__WEBPACK_IMPORTED_MODULE_0 __. Db.collection is not a function

My notes from where I call the DB

import  { db }  from "../firebase/firebase-config";

export const startNewNotes = () => {
  return async ( dispatch, getState ) => {
    const { uid } = getState().auth;

    const newNote = {
      title: '',
      body: '',
      date: new Date().getTime()
    }

    const doc = await db.collection(`${uid}/journal/notes`).add( newNote)
  
    console.log(doc)
  }
}

This is the firebase file in which I am calling firestore, I also tried importing firebase and doing firebase.firestore and it doesn't let me

import 'firebase/firestore';
import 'firebase/auth';

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

// Your web app's Firebase configuration
const firebaseConfig = {
    apiKey: "xx",
    authDomain: "xx",
    projectId: "xx",
    storageBucket: "xx",
    messagingSenderId: "xx",
    appId: "xx"
};

// Initialize Firebase
initializeApp(firebaseConfig);

const db = getFirestore();
const googleAuthProvider = new GoogleAuthProvider();

export{
    db,
    googleAuthProvider
}
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
fanjiooJr
  • 309
  • 2
  • 8
  • The new syntax to add a document looks like: `addDoc(collection(db, uid, 'journal', 'notes'))`. Also check [Add Data to Cloud Firestore](https://firebase.google.com/docs/firestore/manage-data/add-data) – Dharmaraj Sep 17 '21 at 15:15
  • One question, is it telling me that I can't create 3 elements osea ```await setDoc (doc (db, uid, 'journal', 'notes'), newNote)``` but if I get the notes from it osea, ```await setDoc (doc (db, uid , 'journal'), newNote)``` me, how can I add notes – fanjiooJr Sep 17 '21 at 15:25
  • You can use `addDoc()` with `collection()` instead of `setDoc()` to add a document to collection. See my first comment. [What's the pattern for adding new data in web v9?](https://stackoverflow.com/questions/68987326/firestore-whats-the-pattern-for-adding-new-data-in-web-v9) This might be useful too. – Dharmaraj Sep 17 '21 at 15:26
  • Thanks, i'm on the go – fanjiooJr Sep 17 '21 at 16:06

0 Answers0