【发布时间】:2019-06-17 17:27:04
【问题描述】:
我已经尝试解决这个问题好几天了…… 非常感谢任何帮助。
我正在尝试构建一个从 Pub/sub 主题(“ETACheck”)触发的 Firebase 云函数。触发部分工作正常。 调用云函数时,它会扫描 Firebase 数据库以查找“ECT”键等于“”的所有记录。 以下是数据的组织方式:
这里是sn-p的代码:
exports.CheckETAResponse = functions.pubsub.topic('ETACheck').onPublish((message, context) => {
console.log('The function was triggered at ', context.timestamp);
console.log('The unique ID for the event is', context.eventId);
var db = admin.database();
var ref = db.ref('{company}/Requisitions/{store}');
return ref.orderByChild('ECT').equalTo('').on('value')
.then((snapshot) => {
if (snapshot !== null) {
const data = snapshot.val();
console.log('Data',data);
}
else {
console.log('Snapshot is null');
}
return null;
})
.catch((error) => console.log('Error',error))
});
以及来自 Firebase 的错误(数据 = null):
我将查询更改为“.on(‘child_added’),我收到以下错误:
【问题讨论】:
-
请不要链接到文字图片。将文本直接复制到问题中。
-
您在问题中包含了 JSON 树的图片。请将其替换为实际的 JSON 作为文本,您可以通过单击 your Firebase Database console 的溢出菜单 (⠇) 中的 Export JSON 链接轻松获得。将 JSON 作为文本使其可搜索,让我们可以轻松地使用它来测试您的实际数据并在我们的答案中使用它,一般来说这只是一件好事。
-
在这段代码
db.ref('{company}/Requisitions/{store}')中,你觉得{company}和{store}做什么? Firebase 不会自动将它们扩展为任何内容,因此只会将它们视为文字字符串。 -
@Frank van Puffelen,我认为 '{xxx}' 代表通配符。我试图查找任何具有 ECT = '' 的公司或商店的所有记录。
-
这些类型的通配符只能在 Cloud Functions 声明中使用,而不能在数据库的其他路径中使用。我会写一个答案。
标签: javascript firebase-realtime-database google-cloud-functions