简体   繁体   English

在子集合 Firebase v9 中添加数据时出现的问题

[英]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.出现这个错误,我不知道发生了什么,因为我在 Firebase v9 文档中使用了相同的代码参考。

Extra Note: idDoc returns me: YYwcNWMfznuwAB8eN0sU额外注意:idDoc 返回我: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.您用作应用程序参考的代码将创建一个带有自动生成 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你使用 await 首先看到异步函数是 nesscesssry

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

this is working for me这对我有用

try this one for add subcollections in firebase v9试试这个在 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 //如果您不使用 id set primary 作为名称,则无需使用 id 只写数据库唯一的 sugeesttion 的“名称”而不是 id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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