【问题标题】:Firebase messaging send() errors with UnhandledPromiseRejectionWarning带有 UnhandledPromiseRejectionWarning 的 Firebase 消息发送()错误
【发布时间】:2020-02-12 02:57:21
【问题描述】:

我的以下代码有时似乎会出错,但我不确定为什么我无法捕捉到错误并收到UnhandledPromiseRejectionWarning

(node:18) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:18) UnhandledPromiseRejectionWarning: Error: Exactly one of topic, token or condition is required
    at FirebaseMessagingError.FirebaseError [as constructor] (/app/node_modules/firebase-admin/lib/utils/error.js:39:28)
    at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/app/node_modules/firebase-admin/lib/utils/error.js:85:28)
    at new FirebaseMessagingError (/app/node_modules/firebase-admin/lib/utils/error.js:250:16)
    at validateMessage (/app/node_modules/firebase-admin/lib/messaging/messaging.js:359:15)
    at Messaging.send (/app/node_modules/firebase-admin/lib/messaging/messaging.js:500:9)
    at pushLike (/app/build/schema/swipe.js:407:41)
(node:18) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 9)
            if (!_.has(persona_2.profile, 'push_token')) {
                return;
            }

            const { rowCount: badge } = await Chat.knex().raw(newChatCountSql, {
                personaId: context.user.profile.persona[0].id,
            });

            admin.messaging().send({
                token: persona_2.profile.push_token,
                notification: {
                    title: 'my title',
                    body: 'hello world',
                },
                data: {},
                apns: {
                    payload: {
                        aps: {
                            badge,
                            sound: 'default',
                        },
                    },
                },
                // topic: chatId,
            })
                .catch(err => {
                    if (['messaging/invalid-registration-token', 'messaging/registration-token-not-registered'].includes(err.code)) {
                        // push token is no longer valid
                    } else {
                        log.error(err);
                    }
                });

【问题讨论】:

    标签: node.js firebase firebase-cloud-messaging firebase-admin


    【解决方案1】:

    错误是Exactly one of topic, token or condition is required

    所以有时persona_2.profile.push_token 可能为空。

    您能否检查persona_2.profilepush_token 键并且persona_2.profile.push_token 不为空。

                if (!_.has(persona_2.profile, 'push_token') || !persona_2.profile.push_token) {
                    return;
                }
    

    【讨论】:

    • 哦,哇,如果{ push_token: null },我希望_.has(persona_2.profile, 'push_token') 返回false,但它返回true。不知道_.has() 只是一个关键检查。也许我应该改用_.get()_.result()push_token != null。谢谢,并且可能解释了更多的错误,因为我在我的代码中都使用了它!
    猜你喜欢
    • 2018-10-05
    • 2017-01-18
    • 2020-12-05
    • 1970-01-01
    • 2020-12-20
    • 2017-01-19
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多