我如何在我的google-cloud-function(javascript)中正确地写出try-catch?

问题描述 投票:0回答:1

我试图在火库中部署这个功能,从 Flutter聊天应用扩展--推送通知信息 媒介网的指南。我把它复制上去了,但是得到这些错误和警告的回报。我一直在尝试添加try-catch语句,但是失败了。谁能帮帮我?

   7:29  warning  Expected to return a value at the end of arrow function  consistent-return
  19:9   error    Expected catch() or return                               promise/catch-or-return
  24:17  error    Each then() should return a value or throw               promise/always-return
  29:17  error    Expected catch() or return                               promise/catch-or-return
  29:17  warning  Avoid nesting promises                                   promise/no-nesting
  34:25  error    Each then() should return a value or throw               promise/always-return
  46:23  warning  Avoid nesting promises                                   promise/no-nesting
  46:23  warning  Avoid nesting promises                                   promise/no-nesting
  49:31  error    Each then() should return a value or throw               promise/always-return

这个函数。

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('users')
      .where('id', '==', 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
  })
javascript google-cloud-functions try-catch
1个回答
1
投票

得到了一切工作。

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.message

    // Get push token user to (receive)
    admin.firestore().collection('users').where('uid', '==', idTo).get().then(querySnapshot => {
        querySnapshot.forEach(userTo => {
          console.log(`Found user to: ${userTo.data().uid}`)
          if (userTo.data().pushToken) {
            // Get info user from (sent)
            admin.firestore().collection('users').where('uid', '==', idFrom).get().then(querySnapshot2 => {

                querySnapshot2.forEach(userFrom => {
                  console.log(`Found user from: ${userFrom.data().uid}`)
                  const payload = {
                    notification: {
                      title: `You have a message from "${userFrom.data().uid}"`,
                      body: contentMessage,
                      badge: '1',
                      sound: 'default'
                    }
                  }
                  // Let push to the target device

                  admin.messaging().sendToDevice(userTo.data().pushToken, payload).then(response => {
                  return console.log('Successfully sent message:', response)


                    }).catch(error => {
                      console.log('Error sending message:', error)
                    })

                })
                return console.log('failed')
              }).catch(error => {
                 console.log('Error sending message:', error)
              })
          } else {
            console.log('Can not find pushToken target user')
          }
        })
        return console.log('error: invalid path')
      }).catch(error => {
        console.log('Error sending message:', error)
      })




    return null
  })
© www.soinside.com 2019 - 2024. All rights reserved.