为什么会收到无效的集合引用错误?

Why getting Invalid collection reference error?

当用户输入 comment.The 代码时,我正在更新 firebase 中的文档,相同的是:

 const docRef=collection(db,"posts",post.id);
        console.log(docRef);
        updateDoc(docRef,{
          user:localStorage.getItem('Name'),
          title: post.title,
          comment:temppost ,
          upVotesCount: 0,
          downVotesCount: 0,
          createdAt: post.createdAt,
          updatedAt: date.toUTCString(),
          isCommentVisible:false
        });

但是我收到这个错误:

Uncaught (in promise) FirebaseError: Invalid collection reference. Collection references must have an odd number of segments, but posts/PQAvoGjUeX8aPVVZxMNC has 2.

我这里没有使用任何子集合。

你需要 DocumentReference to update a single document that you can create using doc(). The collection() is used to create a CollectionReference:

// use doc() instead of collection()
const docRef = doc(db,"posts",post.id);

同时结帐:

您正在尝试在集合函数中传递文档。 (db,"posts") 是引用集合的方式,(db,"posts",post.id) 是引用文档的方式。所以使用其中之一:

const docRef = doc(db,"posts",post.id);    // 3 arguments for a document
------
const postsRef = collection(db,"posts");   // 2 arguments for a collection
const docRef = (postsRef,post.id);         // in essence 2+1 arguments for a document