Firebase函数onwrite的值有时为null [英] Value of firebase functions onwrite sometimes is null

本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆