首页 > 解决方案 > 如何解决 FCM Cloud 功能 npm 错误?

问题描述

 error    Expected catch() or return                  promise/catch-or-return
 23:13  error    Each then() should return a value or throw  promise/always-return
 28:13  error    Expected catch() or return                  promise/catch-or-return
 28:13  warning  Avoid nesting promises                      promise/no-nesting
 33:21  error    Each then() should return a value or throw  promise/always-return
 45:19  warning  Avoid nesting promises                      promise/no-nesting
 45 :19  warning  Avoid nesting promises                      promise/no-nesting
 48:27  error    Each then() should return a value or throw  '

上面的代码是错误内容(终端Mac OS),由于终端部署,这个错误代码显示。

我关注firebase内容并阅读了一些时间,但我不知道。

Firebase 证明:

"向特定设备发送消息

要将消息发送到特定设备,请转发设备的注册令牌,如下所示。有关注册令牌的更多信息,请参阅关于平台的客户端设置。”

我正在使用 FCM 推送消息,但仅已部署,此代码显示此代码正在向指定人员的 uid 显示推送消息。

一些错误让我很痛苦,这是我的代码(这正是其他人的代码(颤振 FCM 演示)

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

exports.sendNotification = functions.firestore
.document('messages/{groupId1}/{groupId2}/{message}')
.onCreate((snap, context) => {
console.log('----------------start function--------------------')
const doc = snap.data()
console.log(doc)

const idFrom = doc.idFrom
const idTo = doc.idTo
const contentMessage = doc.content

// Get push token user to (receive)
admin
  .firestore()
  .collection('user')
  .where('uid', '==', idTo)
  .get()
  .then(querySnapshot => {
    querySnapshot.forEach(userTo => {
      console.log(`Found user to: ${userTo.data().nickname}`)
      if (userTo.data().pushToken && userTo.data().chattingWith !== idFrom) {
        // Get info user from (sent)
        admin
          .firestore()
          .collection('users')
          .where('id', '==', idFrom)
          .get()
          .then(querySnapshot2 => {
            querySnapshot2.forEach(userFrom => {
              console.log(`Found user from: ${userFrom.data().nickname}`)
              const payload = {
                notification: {
                  title: `You have a message from "${userFrom.data().nickname}"`,
                  body: contentMessage,
                  badge: '1',
                  sound: 'default'
                }
              }
              // Let push to the target device
              admin
                .messaging()
                .sendToDevice(userTo.data().pushToken, payload)
                .then(response => {
                  console.log('Successfully sent message:', response)
                })
                .catch(error => {
                  console.log('Error sending message:', error)
                })
            })
          })
      } else {
        console.log('Can not find pushToken target user')
      }
    })
  })
return null
})

标签: firebasenpmfirebase-cloud-messaging

解决方案


推荐阅读