javascript - 尝试发送通知的 Firebase 函数失败

标签 javascript firebase google-cloud-functions

我正在尝试做以下事情。我有这样的数据库

clients
|___clientnumber: 4
|___name1
    |_device_token: kJ-aguwn7sSHsjKSL....
    |_notificationtrigger: 8

我想做的是,当客户在他的节点“name1”内的我的应用程序中输入一个数字时,当 clientnumber 为 8 时,它将触发通知,因此,客户编号将为 4,然后是 5,然后是 6,依此类推直到 8 点,当它达到 8 时,我编写了这个函数,以便向用户发送通知,告诉他客户编号 8 已准备就绪。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendClientesNotification = functions.database.ref('/clients/clientnumber')
    .onWrite((snapshot, context) => {

        const newClient = snapshot.after.val();
        const oldClient = snapshot.before.val();
        console.log('New client: '+newClient);
        console.log('Old client: '+oldClient);

            // get the user device token
      const getDeviceTokensPromise = admin.database()
      .ref(`/clients/name1/device_token`).once('value');

      const getClientNotificationTrigger = admin.database()
      .ref(`/clients/name1/notificationtrigger`).once('value');


      //I use a promise to get the values above and return, and then I create the notification
      return Promise.all([getDeviceTokensPromise, getClientNotificationTrigger]).then(results => {

        const devicetoken = results[0].val();
        const clientNumberTrigger = results[1].val();

        console.log('New notification for '+devicetoken+' for the number '+clientNumberTrigger);

        if(clientNumberTrigger = newClient){
            const payload = {
                notification: {
                    title: "Alert",
                    body: "alert triggered",
                    icon: "default",
                    sound:"default",
                    vibrate:"true"
                }
            };
        }

        return admin.messaging().sendToDevice(devicetoken, payload);

      });

    });

现在,我将在这里解释我在做什么。

首先我指向引用的clientnumber,并成功获取了它在这里变化前后的值

 const newClient = snapshot.after.val();
            const oldClient = snapshot.before.val();
            console.log('New client: '+newClient);
            console.log('Turno anterior: '+oldClient);

然后我查询客户端中的值以获得 device_tokennotificationtrigger 并使用 promise 获取这两个值,比较它们是否相同然后用当前用户 device_token 返回一个通知

我遇到的错误是这两个

enter image description here

我觉得第一个就是这一行

const clientNumberTrigger = results[1].val();

第二个我不知道为什么会这样,我想我需要在 if 语句之前添加一个条件表达式

我开始使用 firebase 函数,并在此过程中慢慢学习一些 javascript。

最佳答案

如错误所述:您正在尝试使用单个等号 (=) 比较两个值,而您应该使用三个等号:

切换这个:

if(clientNumberTrigger = newClient) { ... }

为此:

if(clientNumberTrigger === newClient) { ... }

至少这个错误应该消失了。

关于javascript - 尝试发送通知的 Firebase 函数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52999258/

相关文章:

javascript - 使用具有 !important 标签的 jquery 更改 div 的背景颜色

javascript - .Net c# webbrowser 填充不带 id 或类名的输入

javascript - 使用 JS - jQuery,如何取消转义 html 并将 `quotes & <>` 放回字符串中?

angular - 调用方法 createUserWithEmailAndPassword 而不执行 signIn

ios - 如何知道主题创建是否为 "submitted"到控制台(Firebase 云消息传递)

Firebase 实时数据库规则(所有人都可以读取但只能编辑所有者)

javascript - 如何在jquery、javascript、HTML5中将base64字符串另存为(PDF、doc、xls、png..)

Firebase 云函数触发器

python - 如何从 Google 云函数返回 JSON

javascript - 带有子集合的 Firestore 查询