Firebase函数:未调用“ onCreate”函数

时间:2019-08-13 11:49:54

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

我正在为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错误。

1 个答案:

答案 0 :(得分:0)

我通过将功能代码从以下位置更改来解决了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变量。