Firebase onWrite 不会触发

一世

我正在尝试使用onWritefordb_uppercaseLog但它不会触发。当我使用时onCreate,它确实会触发。

db_uppercaseLog侦听添加到的新日志/log/:pushId/original并创建日志的大写版本/log/:pushId/uppercase

onCreate((snapshot,context)=>{...} // works fine
onWrite((snapshot,context)=>{...} // did not work

我已经阅读了文档和参考资料。在stackoverflow上也读过类似的问题。但我无法与我的案子联系起来。这应该是教科书的例子吧?

我在这里做错了什么?


// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /log/:pushId/original
// <host>/pushLog?text=
exports.pushLog=functions.https
.onRequest(async(req,res)=>{
  // Grab the text parameter.
  const original = req.query.text;
  // Push the new log into the Realtime Database using the Firebase Admin SDK.
  const snapshot = await admin.database().ref('/log').push({original: original});
  res.status(200).send(
    `<!doctype html><head><title>pushLog</title></head><body>
      <a href="${snapshot.ref.toString()}" target="_blank">${original} was saved</a>
    </body></html>`
  );
});

// Listens for new log added to /log/:pushId/original and creates an
// uppercase version of the log to /log/:pushId/uppercase
exports.db_uppercaseLog=functions.database
.ref('/log/{pushId}/original')
.onWrite((snapshot,context)=>{
  // Grab the current value of what was written to the Realtime Database.
  const original = snapshot.val();
  const uppercase = original.toUpperCase();
  // You must return a Promise when performing asynchronous tasks inside a Functions such as
  // writing to the Firebase Realtime Database.
  // Setting an "uppercase" sibling in the Realtime Database returns a Promise.
  return snapshot.ref.parent.child('uppercase').set(uppercase);
});
编码员

您的问题源于此onWrite,以onCreate 不同的方式处理数据,并且定义与您的问题不同。

onCreate((snapshot,context)=>{...})
onWrite((change,context)=>{...})

特别是,一个Change 对象是一个容器,它包含属性afterbefore,代表触发函数的状态变化。

onWrite更改处理程序一样 - 如果不进行审核,它可以重新触发自身进入无限循环。在上面的代码中,这不是问题,但需要注意。但是当值被删除时你的代码会被触发,所以决定你希望如何处理它。

// Listens for changes at /log/:pushId/original and creates an
// uppercase version of the log at /log/:pushId/uppercase
exports.db_uppercaseLog=functions.database
.ref('/log/{pushId}/original')
.onWrite((change,context)=>{
  let afterSnapshot = change.after;

  // onWrite is triggered on deletion
  if (!afterSnapshot.exists()) {
    console.log('cancelled: entry deleted');
    return; // do nothing (could also delete /log/:pushId/uppercase?)
  }

  // Grab the new value written to the Realtime Database and modify it.
  const newValue = afterSnapshot.val();
  const uppercase = newValue.toUpperCase();

  // Set "../uppercase" and returns its operation Promise.
  return afterSnapshot.ref.parent.child('uppercase').set(uppercase);
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Firebase批处理更新和onWrite触发同步

云功能-创建新文档时不会触发onWrite

Cloud Firestore 不会在通配符中触发 onWrite

Firebase onWrite触发器的云功能:快照.val不是函数

Firebase数据库onWrite的云功能触发两次

删除父级时未调用Firebase onWrite的Cloud Functions触发器

Firebase Cloud Function onWrite完成后,如何触发Android客户端回调?

为什么不在Firebase云功能模拟器上调用firestore onWrite触发器?

比较Firebase数据库的云函数触发器onCreate(),onWrite(),onUpdate(),何时使用?

Firebase云功能Firestore触发器onWrite在本地测试时行为异常

如何在新的Firebase onUpdate或onWrite触发器中更改值?

Firebase 函数 - 在 onWrite() 触发器中找不到父属性

Firebase函数onWrite未被调用

Firebase 函数 .onWrite 不起作用?

Firebase 云函数 onWrite 子值

检索单个值 onWrite firebase 云函数

Firebase onWrite函数可将数据返回给客户端

Firebase的云功能-onWrite发送电子邮件

如何在onwrite事件中获取firebase pushID?

Firebase Cloud Functions - 使用 onWrite 事件更新值

在Firebase onWrite中删除云功能中的当前节点

在.onWrite Firebase云功能中访问doc.id

flutterer Firebase authStateChanges 不会被触发

Firebase存储上载任务继续不会触发

Firebase 不会触发数据更改事件

如何创建在 onWrite 函数之后触发的 PubSub 函数

子集合中的更改是否应该触发onWrite函数?

Firebase存储完成不会触发/不会被回调

AngularFire2-嵌套的FirebaseObjectObservable不会触发嵌套的Firebase调用