Reputation: 6795
I'm trying to create a function that will send a notification whether a document is updated.
I access the document when its updated, and I get the userId who updated that document and a status I need, after that, I just query for the user device token and send a payload, but it seems that is not working, I dont really know if there is some errors in the syntax of the code or some problems with my callbacks.
const functions = require('firebase-functions');
var admin = require("firebase-admin");
{service keys are initialized here}
exports.onOrderCreated = functions.firestore
.document('orders/{orderId}').onUpdate(async (snap,context) => {
var db = admin.firestore();
try{
const orderDataSnap = await snap.ref.get();
var userId = orderDataSnap.data().uid;
var orderStatus = orderDataSnap.data().status;
}catch(error){
return handleErrorToUser(error,snap);
}
let docRef = db.collection('user').doc(userId);
return docRef.get().then(userDoc => {
const deviceToken = userDoc.get('deviceToken')
const payload = {
"data": {
"title": "Your order status here !",
"body": "Your order status is:"+orderStatus,
"icon": "ic_launcher",
"sound": "default",
"click_action": "delete_token"
}
}
return admin.messaging().sendToDevice(deviceToken,payload)
});
});
// Inform about errors (these errors are for testing purposes, we should use StackDriver to log the errors
// to ourselves and let a friendly user error message with this function)
async function handleErrorToUser(tError, snap) {
console.log(tError)
}
Inside orders I have to fetch this two attributes
uid and status
Then , with that uid, fetch the deviceToken inside user reference.
This code gives me
TypeError: Cannot read property 'get' of undefined
> at exports.onOrderCreated.functions.firestore.document.onUpdate (/srv/index.js:17:46)
> at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23)
> at /worker/worker.js:825:24
> at <anonymous>
> at process._tickDomainCallback (internal/process/next_tick.js:229:7)
I'm not so much experienced with javascript, any help will be appreciated.
thanks
Upvotes: 1
Views: 1787
Reputation: 113
I think the problem is in this line:
const deviceToken = userDoc.get('deviceToken')
If 'deviceToken' token is a property from the document data, you should first run the data method, it returns all the document data properties:
const deviceToken = userDoc.data().deviceToken
Upvotes: 2
Reputation: 317487
You've got a few things going wrong in here.
Firstly, the name of the first argument to your onUpdate function should probably not be snap
. As you can see in the documentation for onUpdate, the first argument is a Change object that contains document snapshots for the "before" and "after" state of the document because of the update that happened. That Change object does not have a property called ref
, which is causing your code to fail.
Second, if you're expecting that this code fetch the document that was updated:
const orderDataSnap = await snap.ref.get();
That is totally not necessary here, as the contents of the document are already being delivered in Change object I mentioned before. Just use the data you're being provided, unless you know for sure that it's not going to be what you want.
I suggest reviewing the documentation to get a better sense of how update triggers works.
Upvotes: 2