我正在为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
错误。
答案 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
变量。