getDocs - firebase 反应本机
我想要一份文件并进行更新。 我尝试使用此代码,但他不接受“idDoc”:
const Doc = query(collection(database, "user_veic"),where("email", "==", auth.currentUser?.email),where("kmF", "==", ""));
getDocs(Doc).then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(`${doc.id} => ${doc.data()}`);
const idDoc = doc.id
})
})
.then(
updateDoc(doc(database, "user_veic", idDoc), {
kmF: "teste1",
km: "teste1",
}))
^^^^:FirebaseError:无效的文档引用。文档引用必须有偶数个段,但 user_veic 有 1 个
我尝试过:
const Doc = query(collection(database, "user_veic"),where("email", "==", auth.currentUser?.email),where("kmF", "==", ""));
getDocs(Doc).then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(`${doc.id} => ${doc.data()}`);
const idDoc = doc(database, "user_veic", doc.id)
updateDoc(idDoc, {
kmF: "teste1",
km: "teste1",
})
})
})
^^^^:[未处理的承诺拒绝:TypeError:doc不是函数。 (在'doc(database,“user_veic”,doc.id)'中,'doc'是lh的实例)]
我做错了什么?
I want to get a document and to update.
I tried used this code, but he dont accept the "idDoc":
const Doc = query(collection(database, "user_veic"),where("email", "==", auth.currentUser?.email),where("kmF", "==", ""));
getDocs(Doc).then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(`${doc.id} => ${doc.data()}`);
const idDoc = doc.id
})
})
.then(
updateDoc(doc(database, "user_veic", idDoc), {
kmF: "teste1",
km: "teste1",
}))
^^^^: FirebaseError: Invalid document reference. Document references must have an even number of segments, but user_veic has 1
I tried this:
const Doc = query(collection(database, "user_veic"),where("email", "==", auth.currentUser?.email),where("kmF", "==", ""));
getDocs(Doc).then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(`${doc.id} => ${doc.data()}`);
const idDoc = doc(database, "user_veic", doc.id)
updateDoc(idDoc, {
kmF: "teste1",
km: "teste1",
})
})
})
^^^^: [Unhandled promise rejection: TypeError: doc is not a function. (In 'doc(database, "user_veic", doc.id)', 'doc' is an instance of lh)]
What did i do wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在第一个代码示例中,您在
.forEach()
的回调参数内声明const idDoc
。该变量不存在于回调函数之外。然后,您尝试在updateDoc()
中完全不同的代码块中使用它。此时它是未定义的,因此您会收到一个错误,表明您没有传递足够的参数。在您的第二个代码示例中,它更接近您想要执行的操作,根据错误消息,您似乎没有使用
firebase 中的其余 Firestore 函数导入
。doc
/firestoreIn your first code example, you declare
const idDoc
inside of the callback parameter to.forEach()
. That variable does not exist outside of the callback function. You then try to use it in theupdateDoc()
in a completely different block of code. It is undefined at that point, thus you are getting an error that you aren't passing enough parameters.In your second code example, which is much closer to what you want to do, based on the error message it looks like you aren't importing
doc
with the rest of the Firestore functions fromfirebase/firestore
.RESOLVIDO @Greg 谢谢你
RESOLVIDO @Greg thank you