我正在为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));
});
但是,在上传新内存时不会调用此函数。有人可以帮助我识别问题吗?
编辑:检查云函数日志后,我发现该函数被调用,但发生了TypeError: Cannot read property 'ref' of undefined
错误。
我通过将函数代码从以下位置更改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));
我认为此错误的原因是因为onCreate()
函数的data
Change 对象没有after
变量。