我发现这个奇怪的问题,记录的功能似乎没有用。
我有这个工作代码:
exports.getEvents = functions.https.onRequest((req, res) => {
cors(req, res, () => {
admin.database().ref('events').orderByValue().once('value', function(snapshot) {
res.status(200).send(snapshot.val());
}).catch(error => {
console.error('Error while reading data', error);
res.status(403).send('Error: ' + error);
});
当我从once()
更改为on()
时,我收到错误。
我想要实现的是让服务器在events
发生更改时发送新的JSON有效负载,因为我的应用程序直接读取events.json
并且我只能使用链接来提供数据(所以全部SDK功能已经完成)。我做错了吗?
错误日志:
TypeError: admin.database(...).ref(...).orderByValue(...).on(...).catch is not a function
at cors (/user_code/index.js:24:11)
at cors (/user_code/node_modules/cors/lib/index.js:188:7)
at /user_code/node_modules/cors/lib/index.js:224:17
at originCallback (/user_code/node_modules/cors/lib/index.js:214:15)
at /user_code/node_modules/cors/lib/index.js:219:13
at optionsCallback (/user_code/node_modules/cors/lib/index.js:199:9)
at corsMiddleware (/user_code/node_modules/cors/lib/index.js:204:7)
at exports.getEvents.functions.https.onRequest (/user_code/index.js:19:2)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:26:47)
at /var/tmp/worker/worker.js:635:7
答案 0 :(得分:3)
您已尝试在语句的末尾添加.catch
。 .on
不支持此功能。
请参阅下面的示例代码,以解决您的问题。
admin.database().ref('/somePath')
.orderByValue()
.on('child_added', (snapshot, prevChildKey) => {
console.log(snapshot.val()); // JSON
}, err => {
// Error is thrown here - Not in a .catch
});