gpt4 book ai didi

node.js - Firestore云函数查询数据库: Document does not exist

转载 作者:行者123 更新时间:2023-12-02 20:58:36 26 4
gpt4 key购买 nike

我正在尝试编写一个http云函数,它会触发查询来搜索价格超过100美元的商品,但它总是返回空文档或文档不存在。

规则设置为暂时跳过验证,以便我不需要进行身份验证。

我在这里遗漏了什么吗?

我是 firestore/firebase 的新手。

Collection/Document image here

export const queryForData = functions.https.onRequest((request, response) => {

db.collection('Inventories').where('price','>=',100).get()

.then(snapshot => {
if(snapshot.exists){
const data = snapshot.data();
response.send(data);
}else{
response.send("No docs found!")
}
})
.catch(error => {
console.log(error);
response.status(500).send(error);
});
});

这给了我“没有找到文档!”

最佳答案

您代码中的快照是 QuerySnapshot ,并且没有 contains 属性或 data() 方法。看来您将其与 QueryDocumentSnapshot 混淆了,它确实有一个exists属性和data()方法。

所以你会想做这样的事情:

.then(snapshot => {
if (!snapshot.empty) {
for (let i = 0; i < snapshot.size; i++) {
const data = snapshot.docs[i].data();
response.send(data);
}
} else response.send('No docs found!')
}

关于node.js - Firestore云函数查询数据库: Document does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50303492/

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