【问题标题】:Update another location onUpdate with firebase cloud functions [duplicate]使用firebase云功能更新另一个位置onUpdate [重复]
【发布时间】:2018-11-17 19:19:03
【问题描述】:

我正在尝试使一些数据与 Firebase 云功能保持一致。当主列表中的数据发生变化时,我希望用户收藏列表中的所有数据都发生变化。

目前,我可以调用该函数并获取正在更改的正确数据的日志,但我的查询不起作用。我收到以下错误:

TypeError: ref.update 不是函数 在exports.itemUpdate.functions.database.ref.onUpdate

这是我的代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.itemUpdate = functions.database
  .ref('/items/{itemId}')
  .onUpdate((change, context) => {

  const before = change.before.val();  // DataSnapshot after the change
  const after = change.after.val();  // DataSnapshot after the change

  console.log(after);

  if (before.effects === after.effects) {
    console.log('effects didnt change')
    return null;
  }
  const ref = admin.database().ref('users')
    .orderByChild('likedItems')
    .equalTo(before.title);

  console.log(ref);

  return ref.update(after);
});

我不确定我哪里出错了,感谢所有帮助和指导来解决这个问题!

干杯。

【问题讨论】:

标签: javascript firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

equalTo() 返回一个Query 对象。然后,您尝试在该对象上调用 update()。请注意,在链接的 API 文档中,Query 没有 update() 方法。您不能简单地“更新”尚未执行的查询。您将不得不使用once() 实际执行查询,从返回的promise 中的快照迭代结果,并使用您找到的数据执行进一步的更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 2018-06-25
    • 2020-07-26
    相关资源
    最近更新 更多