Node.js 节点js函数onWrite在google云函数中工作不正常
我有一个node js函数,它尝试在对节点列表进行添加/更新/删除后更新Algolia索引Node.js 节点js函数onWrite在google云函数中工作不正常,node.js,firebase,google-cloud-functions,Node.js,Firebase,Google Cloud Functions,我有一个node js函数,它尝试在对节点列表进行添加/更新/删除后更新Algolia索引 exports.indexlisting_algolia = functions.database.ref('/Listings/{listingId}').onWrite((snapshot, context) => { const index = algolia.initIndex('Listings'); // var firebaseObject = snapshot.
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)));
});
这是我的错误
TypeError:无法读取未定义的属性“val”
在exports.indexlisting\u algolia.functions.database.ref.onWrite(/user\u code/index.js:807:40)
反对。(/user_code/node_modules/firebase functions/lib/cloud functions.js:112:27)
在下一个(本地)
at/user\u code/node\u modules/firebase functions/lib/cloud functions.js:28:71
at_uuwaiter(/user_code/node_modules/firebase functions/lib/cloud functions.js:24:12)
在cloudFunction(/user\u code/node\u modules/firebase functions/lib/cloud functions.js:82:36)
at/var/tmp/worker/worker.js:733:24
在进程中。_tickDomainCallback(internal/process/next_tick.js:135:7)
第807行是这个函数
var firebaseObject = snapshot.data.val();
我做错了什么?我该如何解决这个问题?您使用的是firebase函数模块公开的旧版本API。新的方法要求您接受一个对象,该对象具有
before
和after
属性,作为onWrite和onUpdate触发器的第一个参数。这些属性将是DataSnapshot对象。您的代码当前需要一个DataDeltaSnapshot,这是您在完整1.0发行版之前的测试版中得到的。现在不推荐使用这种方法
你可以读到这本书
有关示例,请参见
您的函数应该更像这样:
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();
})