【问题标题】:Firebase Functions: 'onCreate' function isn't being calledFirebase 函数:未调用“onCreate”函数
【发布时间】:2019-12-19 23:22:51
【问题描述】:

我正在为 Android 开发一个日记应用,它使用 Firebase 的实时数据库来存储 memory 对象。 memory 对象由 title, image, text, list of tags, and more... 组成

当用户向数据库发布新内存时,我想通过将内存索引到 Algolia 来使其可搜索。为此,我将此功能部署到我的 Firebase Functions 项目中:

exports.indexentry = functions.database.ref('/Diary/{userId}/{memoryId}').onCreate(
async (data, context) => {
  const index = client.initIndex(ALGOLIA_MEMORIES_INDEX_NAME);
  const firebaseObject = {
    text: data.val(),
    objectID: context.params.memoryId
  };

  await index.saveObject(firebaseObject);
  return data.after.ref.parent.child('last_index_timestamp').set(Date.parse(context.timestamp));
});

但在上传新内存时不会调用此函数。有人可以帮我识别问题吗?

编辑:查看 Cloud Function 日志后,我发现调用了该函数,但出现了 TypeError: Cannot read property 'ref' of undefined 错误。

【问题讨论】:

  • 您好,您需要向我们提供更多信息……您所说的“未调用此函数”是什么意思?您在 Cloud Function 日志中看到了什么吗?如果您在代码中添加console.log() 怎么办?您确定不写入 Cloud Firestore 而不是实时数据库吗?
  • 你能说明你做了什么应该触发这个功能吗?因此,无论是写入内存的代码,还是您在 Firebase 控制台中执行的操作。
  • 谢谢你,@RenaudTarnec!您的意图帮助我解决了问题。

标签: node.js firebase firebase-realtime-database google-cloud-functions algolia


【解决方案1】:

我通过更改功能代码解决了TypeError: Cannot read property 'ref' of undefined 错误:

return data.after.ref.parent.child('last_index_timestamp').set(Date.parse(context.timestamp));

到:

return data.ref.parent.child('last_index_timestamp').set(Date.parse(context.timestamp));

我认为这个错误的原因是因为data 更改对象onCreate() 函数没有after 变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 2021-02-24
    • 2020-06-12
    • 2018-09-14
    • 2020-06-05
    • 1970-01-01
    相关资源
    最近更新 更多