如何在我的谷歌云函数(javascript)中正确编写try-catch

如何在我的谷歌云函数(javascript)中正确编写try-catch,javascript,google-cloud-functions,try-catch,Javascript,Google Cloud Functions,Try Catch,我正试图从medium.com上的指南中部署firebase此功能。我已经复制/粘贴了它,但得到了这些错误和警告。我一直在尝试添加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

我正试图从medium.com上的指南中部署firebase此功能。我已经复制/粘贴了它,但得到了这些错误和警告。我一直在尝试添加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
  })

一切正常:

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
  })

在最高层次上,研究JavaScript承诺。每次使用承诺时,都会有一个then处理程序,该处理程序在承诺履行时执行。但是,如果承诺失败,您需要一个catch处理程序。快速浏览你的代码,并不是所有的承诺都有捕获处理程序。。如果我能解决这个问题,我会尝试:可以搜索这些错误消息中的每一条,您可以得到它们含义的解释。为了编写有效的云函数代码,您肯定需要理解JavaScript的承诺。但是,我要抓住每一个机会。。所以我不明白为什么它仍然失败,所以我找到了我错过的地方。。现在我只有3个错误,每个错误都应该返回一个值或抛出一个值,我该怎么做?