我试图从云函数中的 firestore 检索一些数据,但什么也没有得到。客户端上的相同查询给出了正确的结果。这可能是小事,但我没有看到问题。我究竟做错了什么?
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
db.settings({ timestampsInSnapshots: true });
exports.myFunction = functions.https.onCall((data, context) => {
const info = getInfo();
//do some stuff with the info
return info;
}
function getInfo() {
const query = db
.collection('info')
.where('table_nr', '==', 1)
.where('number', '<=', 25)
.orderBy('number', 'desc')
.limit(1);
const info = query.get().then(snapshot => {
snapshot.forEach(doc => {
return doc.data();
})
})
return info;
}
当我调用此函数时,我得到:“data:null”
let info = functions.httpsCallable('myFunction')
info().then(res => { console.log(res) })
我尝试了很多不同的选项,例如当我将最后一部分更改为:
const info = query.get().then(snapshot => {
snapshot.docs;
})
我得到一个包含 1 个对象的数组。所以我确信查询中有一个包含数据的文档。 console.log 给了我:
{data: Array(1)}
data: Array(1)
0: {_ref: {…}, _fieldsProto: {…}, _serializer: {…}, _validator: {…},
_readTime: {…}, …}
length: 1
__proto__: Array(0)
__proto__: Object
And:
return query.get().then(querySnapshot => {
if (querySnapshot.empty) {
return { exists: false }
} else {
return { exists: true }
}
})
控制台日志:
{data: {…}}
data:
exists: true
__proto__: Object
__proto__: Object
也许最好补充一点,我为查询创建了一个(工作)索引。