不知道在答应的时候如何回报某物

时间:2019-06-03 06:55:21

标签: javascript firebase react-native es6-promise

我正在使用Firebase,在这里我使用了一些Promise(在React Native应用中)。

我正在尝试部署,但是ESLINT抱怨我的then子句不返回任何内容:

admin.auth().getUser(phone)
    .then(userRecord => {
      const code = Math.floor((Math.random() * 8999 + 1000))

      twilio.messages.create({
        body: 'Your code is ' + code,
        to: phone,
        from: '+4915735984308'
      }, (err) => {
        if (err) { return res.status(422).send(err) }

        admin.database().ref('users/' + phone)
          .update({code: code, codeValid: true}, () => {
            res.send({success: true})
          })
      })
    })
    .catch((err) => {
      res.status(422).send({error: err})
    })

问题是,我不想返回任何东西,只是在数据库中写东西。我尝试了以下方法,但没有成功:

.then(userRecord => {
      const code = Math.floor((Math.random() * 8999 + 1000))

      twilio.messages.create({
        body: 'Your code is ' + code,
        to: phone,
        from: '+4915735984308'
      }, (err) => {
        if (err) { return res.status(422).send(err) }

        admin.database().ref('users/' + phone)
          .update({code: code, codeValid: true}, () => {
            res.send({success: true}, () => {return true})
          })
      }, () => {return true})
    })
    .catch((err) => {
      res.status(422).send({error: err}, () => {return true})
    })

有人可以帮我吗?我应该在哪里写return语句?

1 个答案:

答案 0 :(得分:1)

只需尝试返回最后一行

admin.auth().getUser(phone)
    .then(userRecord => {
      const code = Math.floor((Math.random() * 8999 + 1000))

      return twilio.messages.create({
        body: 'Your code is ' + code,
        to: phone,
        from: '+4915735984308'
      }, (err) => {
        if (err) { return res.status(422).send(err) }

        admin.database().ref('users/' + phone)
          .update({code: code, codeValid: true}, () => {
            res.send({success: true})
          })
      })
    })
    .catch((err) => {
      res.status(422).send({error: err})
    })
相关问题