gpt4 book ai didi

javascript - Firebase云函数检查数据库中是否存在不存在的数据

转载 作者:行者123 更新时间:2023-12-03 02:53:05 26 4
gpt4 key购买 nike

我正在寻找如何检查我的云函数中是否存在文档我的下面的函数在仅增加现有值时工作正常,但现在我尝试添加功能,检查先前的值是否存在以及是否未设置为 1。

我尝试了不同的方法,但得到了诸如“snapshot.exists”或“TypeError:无法在 docRef.get.then.snapshot 处读取未定义的属性“计数””之类的信息

var getDoc = docRef.get()
.then(snapshot => {

if (typeof snapshot._fieldsProto.count !== undefined) {
console.log("haha3", snapshot._fieldsProto.count)

var count = Number(jsonParser(snapshot._fieldsProto.count, "integerValue"));

docRef.set({
count: count + 1
});
}
else {
docRef.set({
count: 1
});
}

});

下面是exists()错误的代码

var getDoc = docRef.get()
.then(snapshot => {

if snapshot.exists() {
console.log("haha3", snapshot._fieldsProto.count)

var count = Number(jsonParser(snapshot._fieldsProto.count, "integerValue"));

docRef.set({
count: count + 1
});
}
else {
docRef.set({
count: 1
});
}

});

此代码的错误是:

TypeError: snapshot.exists is not a function at docRef.get.then.snapshot

最佳答案

看起来docRef要么指向一个集合,要么是一个查询。在这种情况下,您的 snapshot 类型为 QuerySnapshot

要检查查询是否有结果,请使用 QuerySnapshot.empty .

关于javascript - Firebase云函数检查数据库中是否存在不存在的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47755588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com