gpt4 book ai didi

node.js - Firebase 消息传递 send() 错误与 UnhandledPromiseRejectionWarning

转载 作者:太空宇宙 更新时间:2023-11-04 01:18:29 25 4
gpt4 key购买 nike

我的以下代码有时似乎会出错,但我不确定为什么我无法捕获错误并收到 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);
}
});

最佳答案

错误是需要主题、 token 或条件之一

因此有时persona_2.profile.push_token可能为空。

您能否检查一下 persona_2.profile 是否有 push_token key ,并且 persona_2.profile.push_token 不为空。

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

关于node.js - Firebase 消息传递 send() 错误与 UnhandledPromiseRejectionWarning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60180430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com