Javascript Firebase错误:提供给sendToDevice()的注册令牌必须是非空字符串或非空数组

Javascript Firebase错误:提供给sendToDevice()的注册令牌必须是非空字符串或非空数组,javascript,firebase,firebase-cloud-messaging,google-cloud-functions,Javascript,Firebase,Firebase Cloud Messaging,Google Cloud Functions,我想在使用firebase云消息向用户发送好友请求时向他们发送通知,但在发送请求时,会在firebase函数日志中返回此错误 错误:提供给sendToDevice()的注册令牌必须是非空字符串或非空数组。 在FirebaseMessagingError.Error(本机) 在FirebaseMessagingError.FirebaseError[作为构造函数](/user\u code/node\u modules/firebase admin/lib/utils/error.js:39:28

我想在使用firebase云消息向用户发送好友请求时向他们发送通知,但在发送请求时,会在firebase函数日志中返回此错误

错误:提供给sendToDevice()的注册令牌必须是非空字符串或非空数组。 在FirebaseMessagingError.Error(本机) 在FirebaseMessagingError.FirebaseError[作为构造函数](/user\u code/node\u modules/firebase admin/lib/utils/error.js:39:28)

这是我正在使用的java脚本代码

“严格使用”
const functions=require('firebase-functions');
const admin=require('firebase-admin');
admin.initializeApp();
exports.sendNotification=functions.database.ref('/Notifications/{user\u id}/{notification\u id}')。onWrite((更改,上下文)=>{
const user_id=context.params.user_id;
const notification_id=context.params.notification_id;
log('我们有一个针对:'的通知,用户id);
如果(!change.after.val()){
return console.log(“通知已从数据库中删除:”,通知id)
}
const deviceToken=admin.database().ref(`/Notifications/${user\u id}/${notification\u id}`)。once('value');
返回deviceToken.then(结果=>{
const token_id=result.val();
常数有效载荷={
通知:{
标题:“好友请求”,
身体:“你刚收到一个新的朋友请求”,
图标:“默认值”
}
};
返回admin.messaging(){
console.log('这是通知')
});
});

});
听起来像是
token\u id
null
或空字符串。这很可能是因为数据库中不存在
/Notifications/${user\u id}/${notification\u id}
,例如,当目标用户没有令牌时

要防止出现错误消息,只需在使用快照值之前检查快照是否存在:

const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

return deviceToken.then(result => {
    if (!result.exists() || result.val() === "") return false;

    const token_id = result.val();

    const payload = {
        notification: {
            title: "Friend Request",
            body: "You just got a new friend request",
            icon: "default"
        }
    };

    return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{ 
        console.log('this is the notification')
    });
});

听起来像是
token\u id
null
或空字符串。这很可能是因为数据库中不存在
/Notifications/${user\u id}/${notification\u id}
,例如,当目标用户没有令牌时

要防止出现错误消息,只需在使用快照值之前检查快照是否存在:

const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');

return deviceToken.then(result => {
    if (!result.exists() || result.val() === "") return false;

    const token_id = result.val();

    const payload = {
        notification: {
            title: "Friend Request",
            body: "You just got a new friend request",
            icon: "default"
        }
    };

    return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{ 
        console.log('this is the notification')
    });
});

在浪费了许多时间之后,我终于发现了问题所在。现在的问题是我指向了错误的道路。这一行代码就是问题所在

const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');
应该是这样的

const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

在浪费了许多时间之后,我终于发现了问题所在。现在的问题是我指向了错误的道路。这一行代码就是问题所在

const deviceToken = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');
应该是这样的

const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

是的,您可能在数据库中存储了一个空字符串,所以我刚刚为此添加了一个子句。请注意,了解这种情况发生的原因非常重要,因此请检查您的数据库中是否有空令牌,并确定这是否是应用程序的正常用例。如果是这样的话,请确保所有代码都正确处理了它,而不是像这里这样出现错误。我已经检查了我的数据库,所有用户都存储了一个设备令牌。你需要我数据库的屏幕截图吗?我现在需要有人打我耳光。我刚刚发现了我的愚蠢错误,我指向了一个错误的路径是的,你可能在数据库中存储了一个空字符串,所以我只是为此添加了一个子句。请注意,了解这种情况发生的原因非常重要,因此请检查您的数据库中是否有空令牌,并确定这是否是应用程序的正常用例。如果是这样的话,请确保所有代码都正确处理了它,而不是像这里这样出现错误。我已经检查了我的数据库,所有用户都存储了一个设备令牌。你需要我数据库的屏幕截图吗?我现在需要有人打我耳光。我刚刚发现了我愚蠢的错误,我指向了一条错误的道路