0

Trying to get the value from function like

1. const tempIsEmailExistsInDb = isEmailExistsInDb("sample_email@email.com");
2.
3. const isEmailExistsInDb = usrEmail => {
4.      let tempCount;
5.      userModel.countDocuments( {"email": usrEmail}, function (err, count){
6.          tempCount = count;
7.      });
8.      // count > 0 if document exists
9.      console.log("tempCount", tempCount);
10.     return tempCount > 0;
11. };

In this scenario, line 9, 10 and 1 are executing first then only line 6 is executing. Because of this reason, not getting desired value in line 1 and 9

To resolve this, tried to use async await but some where i'm doing mistake. Can any one help in this

Edit: Gone through this but some where i'm doing mistake related to mongoDB

Bahu
  • 1,516
  • 2
  • 28
  • 49
  • Add async and await in your code. You can read about it here https://javascript.info/async-await – Hassan Imam Jan 25 '21 at 06:10
  • 1
    FYI, you cannot make an asynchronous response be synchronous in Javascript. Cannot do it. Instead, you have to learn how to program asynchronously. This [How do I return asynchronous response](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) explains your options for returning an asynchronously retrieved value. Internal to the function you need to use the response inside the callback itself or switch to promise-based database calls and use `async/await`, but even with `await`, you still can't directly return the value from your function. – jfriend00 Jan 25 '21 at 06:13
  • What database are you using (which tells us what your options are for using promises)? – jfriend00 Jan 25 '21 at 06:13
  • I'm using mongoDB – Bahu Jan 25 '21 at 06:17

2 Answers2

1

//Using callback

const tempIsEmailExistsInDb = isEmailExistsInDb("sample_email@email.com", function(res) {
        if (res) {//do something
         }
      });
        
           const isEmailExistsInDb = (usrEmail, cb) => {
            let tempCount;
            userModel.countDocuments( {"email": usrEmail}, function (err, count){
                  tempCount = count;
                  cb((tempCount > 0))
              });
             
         };

Also for using async method

     router.get('/' , async (req, res) => {
        const isEmail = await isEmailExistsInDb("sample_email@email.com");
        if (isEmail > 0) {

        }

     })

 const isEmailExistsInDb = async (usrEmail, cb) => {
                
                let tempCount = await userModel.countDocuments( {"email": usrEmail});

                return tempCount;
    
             };
sourav satyam
  • 980
  • 5
  • 11
0

From your question, it looks like you are happy with a solution that utilizes async/await. In that case, we need to wrap the callback with a promise

const isEmailExistsInDb = async (usrEmail) => {
  return new Promise((resolve, reject) => {
    let tempCount;
    userModel.countDocuments({ email: usrEmail }, function (err, count) {
      if (err) {
        reject(err);
      }
      tempCount = count;
      console.log("tempCount", tempCount);
      resolve(tempCount > 0);
    });
  });
};

Then call it like this

const emailExists = await isEmailExistsInDb();

Note that, async/await is still not a synchronous operation.

shawon191
  • 1,945
  • 13
  • 28
  • 1
    They are using a database that already supports promises so it would be better to use the built-in promise support rather than wrap your own promise around it. – jfriend00 Jan 25 '21 at 06:49