节点js函数onWrite在谷歌云功能中无法正常工作 [英] node js function onWrite is not working properly in google cloud function
问题描述
我有这个节点 js 函数,一旦对节点列表进行添加/更新/删除,它就会尝试更新 Algolia 索引
I have this node js function that attempts to update Algolia index once a add/update/delete is done to node Listings
exports.indexlisting_algolia =
functions.database.ref('/Listings/{listingId}').onWrite((snapshot, context) => {
const index = algolia.initIndex('Listings');
// var firebaseObject = snapshot.data;
var firebaseObject = snapshot.data.val();
console.log("test ",firebaseObject)
firebaseObject.objectID = context.params.listingId;
return index.saveObject(firebaseObject).then(
() =>
snapshot.data.adminRef.parent.child('last_index_timestamp').set(
Date.parse(event.timestamp)));
});
这是我的错误 thraceback
this is my error thraceback
TypeError:无法读取未定义的属性val"在exports.indexlisting_algolia.functions.database.ref.onWrite (/user_code/index.js:807:40)在对象.(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)在下一个(本机)在/user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71在 __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)在 cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)在/var/tmp/worker/worker.js:733:24在 process._tickDomainCallback (internal/process/next_tick.js:135:7)
TypeError: Cannot read property 'val' of undefined at exports.indexlisting_algolia.functions.database.ref.onWrite (/user_code/index.js:807:40) at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) at next (native) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) at /var/tmp/worker/worker.js:733:24 at process._tickDomainCallback (internal/process/next_tick.js:135:7)
第 807 行是这个函数
line 807 is this function
var firebaseObject = snapshot.data.val();
我做错了什么,我该如何解决?
what am I doing wrongly and how can I fix this?
推荐答案
您使用的是由 firebase-functions 模块公开的旧版本 API.新版本要求您接受 Change
对象,具有 before
和 after
属性,作为 onWrite 和 onUpdate 触发器的第一个参数.这些属性将是 DataSnapshot 对象.您的代码当前需要一个 DataDeltaSnapshot,这是您在完整 1.0 版本之前的 beta 版本中获得的.现在已弃用.
You're using an old version of the API exposed by the firebase-functions module. The new one requires that you accept a Change
object, with before
and after
attributes, as the first parameter of onWrite and onUpdate triggers. Those attributes will be DataSnapshot objets. Your code is currently expecting a DataDeltaSnapshot, which is what you got in the beta version before the full 1.0 release. This is now deprecated.
您可以在文档中了解 1.0 版中的 API 更改一个>.
You can read about the API changes in version 1.0 in the documentation.
有关示例,另请参阅数据库触发器文档.
你的函数应该看起来更像这样:
Your function should look more like this:
exports.indexlisting_algolia =
functions.database.ref('/Listings/{listingId}')
.onWrite((change, context) => {
const before = change.before; // snapshot before the update
const after = change.after; // snapshot after the update
const before_data = before.val();
const afater_data = after.val();
})
这篇关于节点js函数onWrite在谷歌云功能中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!