getDocs - firebase 反应本机

发布于 2025-01-10 11:42:03 字数 1267 浏览 0 评论 0原文

我想要一份文件并进行更新。 我尝试使用此代码,但他不接受“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十级心震 2025-01-17 11:42:03

在第一个代码示例中,您在 .forEach() 的回调参数内声明 const idDoc。该变量不存在于回调函数之外。然后,您尝试在 updateDoc() 中完全不同的代码块中使用它。此时它是未定义的,因此您会收到一个错误,表明您没有传递足够的参数。

在您的第二个代码示例中,它更接近您想要执行的操作,根据错误消息,您似乎没有使用 firebase 中的其余 Firestore 函数导入 doc /firestore

In 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 the updateDoc() 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 from firebase/firestore.

○愚か者の日 2025-01-17 11:42:03

RESOLVIDO @Greg 谢谢你

const Doc = query(collection(database, "user_veic"),where("email", "==", auth.currentUser?.email),where("kmF", "==", ""));

getDocs(Doc).then((querySnapshot) => {
    let values = null;
    querySnapshot.forEach((doc) => {
        console.log(`${doc.id} => ${doc.data()}`);
        values = doc.id;
    });

    var transactionUpdate = database.collection("user_veic").doc(values);
    transactionUpdate.update({
        kmF: kmF,
    })    
})

RESOLVIDO @Greg thank you

const Doc = query(collection(database, "user_veic"),where("email", "==", auth.currentUser?.email),where("kmF", "==", ""));

getDocs(Doc).then((querySnapshot) => {
    let values = null;
    querySnapshot.forEach((doc) => {
        console.log(`${doc.id} => ${doc.data()}`);
        values = doc.id;
    });

    var transactionUpdate = database.collection("user_veic").doc(values);
    transactionUpdate.update({
        kmF: kmF,
    })    
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文