Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Firebase Firestore中执行简单的选择查询_Javascript_Firebase_Google Cloud Functions_Google Cloud Firestore_Select Query - Fatal编程技术网

Javascript 在Firebase Firestore中执行简单的选择查询

Javascript 在Firebase Firestore中执行简单的选择查询,javascript,firebase,google-cloud-functions,google-cloud-firestore,select-query,Javascript,Firebase,Google Cloud Functions,Google Cloud Firestore,Select Query,如何在Firebase Firestore中执行简单搜索以检查集合中是否存在记录?我在文档中看到过这段代码,但它并不完整 // Create a reference to the cities collection var citiesRef = db.collection('cities'); // Create a query against the collection var queryRef = citiesRef.where('state', '==', 'CA'); 在我的用例

如何在Firebase Firestore中执行简单搜索以检查集合中是否存在记录?我在文档中看到过这段代码,但它并不完整

// Create a reference to the cities collection
var citiesRef = db.collection('cities');

// Create a query against the collection
var queryRef = citiesRef.where('state', '==', 'CA');
在我的用例中,我想检查给定号码的拒绝联系人集合。这是我的密码

const collectionRef = db.collection('rejectedContacts');
    const queryRef = collectionRef.where('contact','==',phone);

    let contactRejected: boolean = queryRef.get.length>0 ;
    if (contactRejected) {
        return response.json(
            {
                status: 0,
                message: `Contact , ${phone} is blocked. Please try again with another one.`,
                result: null
            });
    }
我已经用一个被拒绝的数字检查了这个函数,我在集合中手动添加了这个数字,但是它没有用被拒绝的消息进行响应。如何使用select查询获取计数或行本身。我的代码有什么问题

更新1 正如@Matt R所建议的,我用这个更新了代码

let docRef = db.collection('rejectedContacts').where('contact', '==', phone).get();
    docRef.then(doc => {
        if (!doc.empty) {
            return response.json(
                {
                    status: 0,
                    message: `Contact , ${phone} is blocked. Please try again with another one.`,
                    result: null
                });
        } else {
            return response.json(
                {
                    status: 0,
                    message: `Contact , ${phone} is not blocked`,
                    result: null
                });
        }
    })
        .catch(err => {
            console.log('Error getting document', err);
        });
但当使用现有号码进行检查时,它返回时未被阻止

编辑2

return collectionRef.select('contact').where('contact', '==', phone).get()
        .then(snapShot => {
            let x : string = '['
            snapShot.forEach(doc => {
                console.log(doc.id, '=>', doc.data());
                x+= `{${doc.data}},`;
            });
            return response.send(x);
        });
它仅从此行返回x的值

let x : string = '['
Firestore的get()方法返回承诺和响应,我将尝试使用这种格式来处理文档是否存在。在进行故障排除时,此格式还可以提供更好的错误消息

编辑/更新:使用where子句返回必须迭代的快照,因为它可以返回多个文档。请参阅此处的文档:

使用您的示例更新了代码

var collectionRef=db.collection('rejectedContacts');
var query=collectionRef.where('contact','=',phone).get()
。然后(快照=>{
snapshot.forEach(doc=>{
console.log(doc.id'=>',doc.data());
});
})
.catch(错误=>{
console.log('获取文档时出错',err);

});更新了示例,对“where”进行了正确处理,没有任何更改,控制台中也没有打印任何内容,因此我返回了循环结果作为响应,它只显示
[
,我将更新我的问题