Result always undefined when return the result Nodejs

0 votes

Hello, I'm having a problem. I'd like to return the result after getting the target content on the getnada email, but it always returns undefined. Can someone assist me?

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

Here is the code for my procedure for obtaining information. The information I'm looking for

However, when I return the getOTPEmail function, it always returns undefined.

This is the outcome I announce.

const getDataOTP = await getEmailData(getExtention)
console.log(getDataOTP)

Could someone please explain why this code isn't working?

May 31 in Node-js by Vaani
• 7,020 points
2,936 views

1 answer to this question.

0 votes

One easy way to do it would be to move the code from the .then callback "outside" as follow :

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

Also, you will notice that this part const getEmailCount = await res.data.msgs.length probably doesn't work as you would expect because length isn't a promise.

But why isn't it working as is ?

Using .then is using a callback on the promise return by axios.get so your getEmailData function doesn't return anything. It is calling axios.get which is asynchronous and provide a callback once the promise has been resolved, and leaving.

Is there a way to make this work using .then ?

There is, with the following steps :

  • initialize a Promise in getEmailData
  • remove the await in front of axios.get call.
  • call resolve from the initialized Promise in .then callback

Pseudo-Code :

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

But it is way easier to write, easier to read and cleaner to use await, otherwise it's becoming a callback hell :)

answered Jun 7 by Neha
• 8,940 points

Related Questions In Node-js

0 votes
1 answer

How to get the _id of inserted document in Mongo database in NodeJS?

Hello @kartik, A shorter way than using second ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,800 points
9,604 views
0 votes
1 answer

How to get path from the request in nodejs?

Hello @kartik, Try this out: var http = require('http'); var ...READ MORE

answered Oct 14, 2020 in Node-js by Niroj
• 82,800 points
2,797 views
0 votes
1 answer

How to set the content-type of request header when using Fetch APi?

Hello @kartik, You need to create a fetch ...READ MORE

answered Oct 15, 2020 in Node-js by Niroj
• 82,800 points
4,579 views
0 votes
1 answer

How to hide password in the nodejs console?

Hello @kartik, To hide your password input, you ...READ MORE

answered Nov 30, 2020 in Node-js by Niroj
• 82,800 points
2,752 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,112 views
0 votes
1 answer
0 votes
1 answer

Start script missing error when running npm start

It seems that there is an undefined ...READ MORE

answered Feb 10 in Java by Soham
• 9,670 points
2,974 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP