Firebase函数onwrite的值有时为null

编程入门 行业动态 更新时间:2024-10-26 16:26:27
本文介绍了Firebase函数onwrite的值有时为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试实现Android订购系统,并且正在使用实时数据库.要发送通知,我使用JavaScript中的Firebase函数.

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.

每个用户及其ID,发送通知的令牌以及其他数据都存储在数据库中.我要尝试的是检测用户何时收到订单并向他们发送通知.

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.

问题出现在这里.在某些情况下会发送通知,而在其他情况下则不会.查看我的"sendNotif"函数的日志,我看到错误是这样的:

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)

我的代码:

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); }); });

第133行是: const compradorId = solicitud.idcomprador .我不明白为什么有时有时会起作用,有时却不起作用.

Line 133 is this: const compradorId = solicitud.idcomprador. I can't understand why it sometimes works and sometimes it doesn't.

推荐答案

onWrite将触发与触发器位置匹配的任何更改.这意味着它将触发任何创建,更新或删除操作.

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.

当触发事件是新数据的创建时, change.before 将是未定义的(之前没有数据),而 change.after 将包含一个快照.

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.

当触发事件是数据删除时, change.before 将包含一个快照,而 change.after 将是未定义的.

When the triggering event is a deletion of data, change.before will contain a snapshot, and change.after will be undefined.

您的代码盲目地假设 change.after 包含快照.删除数据很有可能会触发您的功能,但失败了,因为那不是设计的目的.

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.

使用onWrite时,有义务检查并适当处理此情况.如果您不关心删除事件,请考虑使用onCreate来捕获新数据.

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.

更多推荐

Firebase函数onwrite的值有时为null

本文发布于:2023-11-05 15:47:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561242.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Firebase   null   onwrite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!