Javascript 返回结果Nodejs时,结果始终未定义

Javascript 返回结果Nodejs时,结果始终未定义,javascript,node.js,Javascript,Node.js,嗨,我这里有一些问题,我想在getnada电子邮件上获取目标内容后返回结果,但它总是返回未定义的内容,有人能在这里帮助我吗 const getEmailData = async (getExtention) => { // return getExtention await axios .get(`https://getnada.com/api/v1/inboxes/${getExtention}`) .then(async (res) => {

嗨,我这里有一些问题,我想在getnada电子邮件上获取目标内容后返回结果,但它总是返回未定义的内容,有人能在这里帮助我吗

const getEmailData = async (getExtention) => {
  //   return getExtention
  await axios
    .get(`https://getnada.com/api/v1/inboxes/${getExtention}`)
    .then(async (res) => {
      const getEmailCount = await res.data.msgs.length
      if (parseInt(getEmailCount) >= 1) {
        // console.log('Berhasil berhasil Hore')
        const getDataEmail = await axios.get(
          `https://getnada.com/api/v1/messages/html/${res.data.msgs[0].uid}`
        )
        if (getDataEmail) {
          console.log('Data berhasil')

          const getOTPEmail = await Promise.all(getDataEmail.data)

          return getOTPEmail
        } else {
          console.log('Data Gagal')
        }
      }
    })
    .catch(() => {
      getEmailData(getExtention)
    })
}
这是我获取想要的内容的过程代码

但是在我返回
getOTPEmail
之后,它总是未定义的

我宣布的结果是这样的

const getDataOTP = await getEmailData(getExtention)
console.log(getDataOTP)
let getDataEmail ;
if (parseInt(getEmailCount) >= 1) {
        // console.log('Berhasil berhasil Hore')
        getDataEmail = await axios.get(
          `https://getnada.com/api/v1/messages/html/${res.data.msgs[0].uid}`
        )
        if (getDataEmail) {
          console.log('Data berhasil')

          const getOTPEmail = await Promise.all(getDataEmail.data)

          return getOTPEmail
        } else {
          console.log('Data Gagal')
        }

有人能解释一下这段代码有什么问题吗?

一个简单的方法是将代码从
中移出。然后
回调到“外部”,如下所示:

        const getEmailData = async (getExtention) => {
      //   return getExtention

      try {
      let res = await axios
        .get(`https://getnada.com/api/v1/inboxes/${getExtention}`);
         const getEmailCount = await res.data.msgs.length
              if (parseInt(getEmailCount) >= 1) {
                // console.log('Berhasil berhasil Hore')
                const getDataEmail = await axios.get(
                  `https://getnada.com/api/v1/messages/html/${res.data.msgs[0].uid}`
                )
                if (getDataEmail) {
                  console.log('Data berhasil')
        
                  const getOTPEmail = await Promise.all(getDataEmail.data)
        
                  return getOTPEmail
                } else {
                  console.log('Data Gagal')
                }
              }
      } catch (e) {
        getEmailData(getExtension);
      }
    }
另外,您会注意到,这部分
const getEmailCount=await res.data.msgs.length
可能不会像您预期的那样工作,因为长度不是一个承诺

但为什么它不能正常工作?

使用
。然后
对axios的承诺返回使用回调。get这样你的
getEmailData
函数就不会返回任何内容。它正在调用
axios.get
,它是异步的,在承诺得到解决后提供回调,然后离开

有没有一种方法可以使用。那么?

有以下步骤:

  • getEmailData
  • 移除
    axios前面的
    wait
    。获取
    呼叫
  • 中初始化的承诺调用
    resolve
    。然后调用
    callback
伪代码:

const getEmailData = async (getExtension) => {
    return new Promise((resolve) => {
     axios.get().then((res) => {
    resolve(res);
   })
  });
}

但是它更容易编写,更容易阅读,使用起来更干净,否则它会变成一个回调地狱:)

我想错误就在这里

if (parseInt(getEmailCount) >= 1) {
        // console.log('Berhasil berhasil Hore')
        const getDataEmail = await axios.get(
          `https://getnada.com/api/v1/messages/html/${res.data.msgs[0].uid}`
        )
        if (getDataEmail) {
          console.log('Data berhasil')

          const getOTPEmail = await Promise.all(getDataEmail.data)

          return getOTPEmail
        } else {
          console.log('Data Gagal')
        }
因为您在if语句中定义了“getDataEmail”变量,并且希望在其外部使用它。 所以,我认为你必须这样做

const getDataOTP = await getEmailData(getExtention)
console.log(getDataOTP)
let getDataEmail ;
if (parseInt(getEmailCount) >= 1) {
        // console.log('Berhasil berhasil Hore')
        getDataEmail = await axios.get(
          `https://getnada.com/api/v1/messages/html/${res.data.msgs[0].uid}`
        )
        if (getDataEmail) {
          console.log('Data berhasil')

          const getOTPEmail = await Promise.all(getDataEmail.data)

          return getOTPEmail
        } else {
          console.log('Data Gagal')
        }

我不确定整个代码,但是您没有从
getEmailData
函数返回任何内容。尝试在
wait axios
之前添加
return
,作者试图访问的getDataEmail在其定义的范围之外的何处?对我来说,它似乎总是在正确的范围内使用它,即使在你修改过的代码中,外部的声明也不会改变任何东西。明白了吗,伙计,你的代码工作我很感激,伙计,谢谢你的帮助,它的工作,但我有一个问题,在检查邮件计数我会搜索如何设置超时之前,检查电子邮件count@Maszgalang不客气,别忘了接受答案(并向上投票):)非常感谢mate upvote太棒了,在出现错误后如何重复该函数@sorikairo如果getEmailCount为1,则该函数工作正常,但如果为0,则结果不会返回任何内容,只返回console.log(success)@Maszgalang在axios.get周围使用try-catch。我正在编辑我的答案以向您展示!