Ошибки отправки () сообщений Firebase с помощью UnhandledPromiseRejectionWarning - PullRequest
1 голос
/ 12 февраля 2020

У меня есть следующий код, который иногда выдает ошибку, но я не уверен, почему не могу поймать ошибку, и получаю 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);
                    }
                });

1 Ответ

0 голосов
/ 12 февраля 2020

Ошибка: Exactly one of topic, token or condition is required.

Так что иногда persona_2.profile.push_token может быть нулевым.

Не могли бы вы проверить, persona_2.profile имеет push_token ключ и persona_2.profile.push_token не является нулевым.

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