Firestore云功能查询数据库:文档不存在

Raw*_*anN 3 node.js firebase typescript google-cloud-functions google-cloud-firestore

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

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

我在这里想念什么吗?

我是Firestore / firebase的新手。

此处的收藏/文件图片

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);
    });
});
Run Code Online (Sandbox Code Playgroud)

这给了我“找不到文档!”

Geo*_*off 5

您的代码中包含的快照是QuerySnapshot,没有现存属性或data()方法。似乎您将其与QueryDocumentSnapshot混淆,后者确实具有一个exist属性和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!')
}
Run Code Online (Sandbox Code Playgroud)