问题
I'm trying to implement an Android ordering system and I'm using Realtime Database. To send notifications I'm using Firebase Functions in JavaScript.
Each user is stored in the database along with their id, token to send the notification, among other data. What I'm trying to do is detect when a user receives an order and send them a notification.
The problem appears here. In some cases the notification is sent and in others it is not. Viewing the logs of my "sendNotif" function I see that the error is this:
TypeError: Cannot read property 'idcomprador' of null
at exports.sendNotif.functions.database.ref.onWrite (/user_code/index.js:133:32)
at Object.<anonymous> (/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:827:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
My code:
exports.sendNotif = functions.database.ref('/Usuarios/{vendedorId}/Solicitudes/Venta/{pushId}').onWrite((change, context) => {
// Grab the current value of what was written to the Realtime Database.
const solicitud = change.after.val();
const compradorId = solicitud.idcomprador
const pedidoId = solicitud.idpedido
const vendedorId = solicitud.idvendedor
var solicitudTokens = [vendedorId];
// Notification details.
const payload = {
notification: {
title: `Order`,
body: `order`,
icon: 'photoURL',
tag: 'solicitudventa',
sound: 'default',
clickAction: 'ACTIVITY_ORDER',
badge: '2'
},
data: {
extra: 'extra_data',
},
};
const options = {
collapseKey: 'demo',
contentAvailable: true,
priority: 'high',
timeToLive: 60 * 60 * 24,
};
var ref = admin.database().ref(`Usuarios/${vendedorId}/token/result/token`);
return ref.once("value", function(snapshot){
admin.messaging().sendToDevice(snapshot.val(), payload)
},
function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
});
Line 133 is this: const compradorId = solicitud.idcomprador. I can't understand why it sometimes works and sometimes it doesn't.
回答1:
onWrite will trigger for any change that was matched by the location of the trigger. This means it will trigger for any create, update, or delete.
When the triggering event was a creation of new data, change.before
will be undefined (the was no data before), and change.after
will contain a snapshot.
When the triggering event is a deletion of data, change.before
will contain a snapshot, and change.after
will be undefined.
Your code is blindly assuming that change.after
contains a snapshot. It's entirely possible that deleting data is triggering your function, and it's failing because that's not what it was designed to handle.
When using onWrite, it's your obligation to check this case and handle it appropriately. If you don't care about delete events, consider using onCreate instead to just capture new data.
来源:https://stackoverflow.com/questions/55250888/value-of-firebase-functions-onwrite-sometimes-is-null