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, 2022 in Node-js by Vaani
• 7,070 points
8,387 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, 2022 by Neha
• 9,020 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,880 points
13,722 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,880 points
4,472 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,880 points
7,922 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,880 points
5,085 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,900 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,422 views
0 votes
1 answer

Start script missing error when running npm start

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

answered Feb 10, 2022 in Java by Soham
• 9,710 points
4,408 views
0 votes
1 answer

What is the role of Nodejs and Express in a MERN stack web application when GraphQL is also used?

Node.js is a JavaScript runtime environment, which ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
2,584 views
0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
3,844 views
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