简体   繁体   English

我正在尝试从 ReactJS 更新 Firestore 文档中的字段

[英]I am trying to update a field inside a document in Firestore from ReactJS

I am trying to update a field inside a document in firestore from reactjs and its showing an error, even though others did it and it worked for them.我正在尝试从 reactjs 更新 firestore 中文档中的一个字段并显示错误,即使其他人这样做并且对他们有用。

我的代码

错误

app.js应用程序.js

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

postpage.js postpage.js

import { auth, app, db } from "../../firebase/config";
import { collection,  getDocs,  addDoc,  deleteDoc,  doc, updateDoc } from "firebase/firestore";

const [allPosts, setPosts] = useState([]);

 const likePost = async (id, likes) => {
 await updateDoc(doc(db, "posts", id), {
   likes: likes + 1,
 });
};

  const getPosts = async () => {
   const data = await getDocs(postsControllerRef);
   setPosts(data.docs.map((doc) => ({ ...doc.data(), id: doc.id 
  })));
  setLoading(false);
 };

return(
{allposts.map((post, index) => (
 <button 
  onClick={() =>
   likePost(post.postid, post.likes)
  }
 >Like</button>
))}
)

The id is a number but doc() function takes only string path segments. id是一个数字,但doc() function 仅采用字符串路径段。 Also, you must pass the documentId to update a specific document.此外,您必须传递documentId才能更新特定文档。 Try changing the following line:尝试更改以下行:

onClick={() =>
 likePost(post.id, post.likes) // pass .id and not .postid
}

If postid is the document ID itself, then try:如果postid是文档 ID 本身,则尝试:

const likePost = async (id, likes) => {
  await updateDoc(doc(db, "posts", String(id)), {
    likes: likes + 1,
  });
}; 

Also, as @FrankVanPuffelen commented, you can use increment() instead as shown below:此外,正如@FrankVanPuffelen 评论的那样,您可以使用increment()代替,如下所示:

import { increment } from "firebase/firestore"

await updateDoc(doc(db, "posts", String(id)), {
  likes: increment(1),
});

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

相关问题 我无法从云 firestore 访问文档字段并在 flutter 中的 statefulwidget class 中显示它 - I am unable to access document field from cloud firestore and show it in a statefulwidget class in flutter 如何从 Firestore 文档获取更新? ReactJS - How can I get updates from a Firestore document? ReactJS 如何使用帮助文档 ID 更新 Cloud firestore 文档字段? - How can i Update Cloud firestore document Field with the help DocumentID? Flutter Firestore 更新集合内的文档名称 - Flutter Firestore update document name inside of collection 如何将 Firestore 文档引用存储为 nextjs 中的字段? - How do I store a Firestore document reference as a field from nextjs? 如何更新 Firestore 文档中特定字段的值? - How to update value of a specific field in Firestore document? REACT JS FIREBASE FIRESTORE- 我如何更新文档中的字段? - REACT JS FIREBASE FIRESTORE- how can i update my field in a document? 如何使用 Cloud firestore 中的文档 ID 访问文档字段? - How can I acess a document field using document id from cloud firestore? 我在尝试将数据从云 Firestore 带到我的 flutter 应用程序时遇到此错误 - I am getting this error while am trying to bring data from cloud firestore to my flutter app 从 Firestore 中的一个文档中获取一个字段 - Get one field from one document in Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM