简体   繁体   中英

Problem when add data in Subcollection Firebase v9

await addDoc(collection(db, 'users', idDoc, 'notes'), {
    title: noteTitle,
    body: 'comentario por defecto.',
    timestamp: serverTimestamp(),
});

错误

我的数据库结构

When I trying to add data in my sub collection 'notes'. Appear this mistake, I don't know what is happening because I using the same code reference in Firebase v9 Documentation.

Extra Note: idDoc returns me: YYwcNWMfznuwAB8eN0sU

It seems you were in the right direction but I would recommend checking how to reference subcollections and then using that reference to write data into it.

It won't hurt to check your imports and dependencies as well.

 import { doc } from "firebase/firestore"; const messageRef = doc(db, "rooms", "roomA", "messages", "message1");

This translated to your project would be:

const notesRef = doc(db, 'users', idDoc, 'notes'); 

Next, you need to check the Add a document section as well. The code you are using as a reference applied to your app would create a new note with an auto-generated id. In the reference, "cities" would correspond to your "notes" reference.

Finally, if you substitute this, it would look like this:

const noteRef = await addDoc(collection(db, notesRef), {
    title: noteTitle,
    body: 'comentario por defecto.',
    timestamp: serverTimestamp() // You also had an extra coma here
});
console.log("Note written with ID: ", noteRef.id);

The console log is optional

you use await first see async function is nesscesssry

addDoc(collection(db, 'Institute', 'Private', 'College'), { name:"tunio" })

this is working for me

try this one for add subcollections in firebase v9

`function uploadData() {
    var id = 'id_' + Math.random().toString(36).substring(2) + (new Date()).getTime().toString(36); //create id for uniqness

    setDoc(doc(db, "Institute", `college`, `city`, `${id}`), {
        Name: "CAted college"
    })}`

//if you not use id set primary as name then no need to use id only write "name" of sugeesttion that unique for data base instead of id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM