Firebase admin.ref.on()不工作,而once()工作正常

Firebase admin.ref.on()不工作,而once()工作正常,firebase,firebase-realtime-database,Firebase,Firebase Realtime Database,我发现这个奇怪的问题,文档化的特性似乎不起作用 我有这个工作代码: exports.getEvents = functions.https.onRequest((req, res) => { cors(req, res, () => { admin.database().ref('events').orderByValue().once('value', function(snapshot) { res.status(200).send(snapshot.

我发现这个奇怪的问题,文档化的特性似乎不起作用

我有这个工作代码:

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()
时,会出现错误

我想要实现的是,当
事件发生更改时,服务器发送新的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

您试图在语句末尾添加一个
.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
  });

您试图在语句末尾添加一个
.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
  });

什么错误?您可以共享安全规则和示例数据吗?我认为这与安全无关,因为
once()
方法可以完美地工作。我用仪表板上的错误日志更新了我的问题-也许这会有帮助。什么错误?您可以共享安全规则和示例数据吗?我认为这与安全无关,因为
once()
方法可以完美地工作。我用仪表板上的错误日志更新了我的问题-也许这会有帮助。它现在正在工作,但似乎在数据库发生更改时不会更新-也许Firebase不支持这一点,但为什么他们要在()上实现
?它只会在
/somePath
中添加新的子项时启动。它不会触发更新,只会触发一次新的写入操作。尝试了-不工作:/refresh new data(刷新新数据)后才会显示。它现在工作,但似乎在数据库发生更改时不会更新-可能Firebase不支持,但为什么他们要在()上实现
然后呢?它只会在将新的子对象添加到
/somePath
时触发。它不会触发更新,只会触发一次新的写入操作。请尝试该操作-不工作:/refresh new data(刷新新数据)显示后。