Reputation: 1659
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)));
});
this is my error thraceback
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)
line 807 is this function
var firebaseObject = snapshot.data.val();
what am I doing wrongly and how can I fix this?
Upvotes: 0
Views: 478
Reputation: 317497
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.
You can read about the API changes in version 1.0 in the documentation.
Please also see the documentation for database triggers for examples.
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();
})
Upvotes: 1