我的firebase函数中有一个firestore查询,该查询用于检索文档中是否具有特定值的文档(例如:如果name字段等于“ name”)。我已经编写了代码并将函数声明为function.https.onCall,因此可以在我的应用程序中调用它。调用该函数有效,但是启动后它什么也没做。 这是我编写的引起问题的查询:
let query = admin.firestore().collection('...').where('...', '==', value).orderBy('...').limit(1);
query.get().then(snapshot => {
let ... = snapshot[0];
这是我的函数声明:
exports.functionName = functions.https.onCall((data, context) => {
该函数应该做的是记录从调用代码传递给它的内容,执行查询(当前使用有效数据进行测试),然后继续执行该函数的其余部分。现在,它什么也没做,但是当我删除where函数时,它却无法获取我正在寻找的特定文档。感谢您对我的问题的任何见解。
答案 0 :(得分:0)
基于您提供的代码示例,我假设您正在使用Nodejs Cloud Function。您需要做的是列出集合中的所有文档,并从每个文档中获取所需的数据字段。
您的package.json
文件应如下所示:
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"firebase-admin": "^6.5.1"
}
}
然后您需要与Firebase建立连接:
const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.applicationDefault()
});
var db = admin.firestore();
并运行以下代码以列出集合中的所有文档:
//From collection -> COLLECTION_NAME
db.collection('COLLECTION_NAME').get()
.then((snapshot) => {
snapshot.forEach((doc) => {
//Checking each document in that collection
//Log the document ID and the ClientID field from each document in the collection
console.log(doc.id, '=>', doc.data().clientID);
});
})
.catch((err) => {
console.log('Error getting documents', err);
});
//Return a message to the main function to print a message on browser notifying about the successful operation
return 'Finished checking';
使用您集合的名称代替COLLECTION_NAME
。而且,请使用您要查找的字段,而不是clientID
中提交的doc.data().clientID
。在此处执行if语句。
例如:
if (doc.data().name == 'name'){
//Do something
}