0

I want to check the data is exist in a database. If suppose the data exist the value of k is set to 1

global.k = 0
va roll = {roll0:"1616",roll1:"234"}
for (let i = 0; i < inputcount; i++) {
  var obj1 = roll["roll" + i];

  const find = async() => {
    const item = await RegisterUser.find({ eventname: event_name, rollno: obj1 })
    if (item.length != 0) {
      global.k = 1
      console.log(global.k)
    } else {
      global.k = -1
      console.log(global.k)
    }
  }
  find()
}
  console.log(global.k)
if(global.k==0){
  // code for save
}

After if loop is executed, the value is not print as 1 or -1.Actually, I think the problem is in async. Please help me

Rajendran bala
  • 87
  • 1
  • 11

1 Answers1

1

You call find but don't await its result. Better make that async function something that wraps the whole loop:

const find = async() => {
  global.k = 0    
  for (let i = 0; i < inputcount; i++) {
    var obj1 = roll["roll" + i];    
    const item = await RegisterUser.find({ eventname: event_name, rollno: obj1 })
    if (item.length != 0) {
      global.k = 1
      console.log(global.k)
    } else {
      global.k = -1
      console.log(global.k)
    }
  }
  console.log(global.k);
  if (global.k == 0) {
     // save...
  }
}
find() // Don't put any code below this line. Put it at the end of the function above

NB: The algorithm itself looks strange, because every assignment to global.k overwrites the previous value that was assigned. The name for variable k does also not help to understand what its purpose is.

If you need it to be 1 when all queries return a match, then add a break here:

    } else {
      global.k = -1
      console.log(global.k)
      break;
    }

If you need it to be 1 when at least one query return a match, then add a break here:

    if (item.length != 0) {
      global.k = 1
      console.log(global.k)
      break;
    }

Also the check at the end if (global.k == 0) is strange, because that is equivalent to doing if (inputcount == 0). So make sure to check your logic.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • When I console inside `then` the value of k is 1 or -1 sir, but outside `then` the value is 0. Actually `then` is executed at last, before that outside function is excuted sir – Rajendran bala Mar 02 '19 at 13:08
  • That is exactly how it is supposed to work. You *must* do it within a `then` callback, because the database needs time to do its job. Alternatively, you must put *all* your code in an `async` function. Read about asynchronous code. You must go all the way and make all your code work with asynchrony. – trincot Mar 02 '19 at 13:09
  • sir please check the git repo sir https://gitlab.com/BalaRajendran/orlia2k19/blob/master/server/routes/index.js , I want to check the multiple data are exist in database all that data is not existed means the k value is 0 after that i will check the value of k if k is 0 i will save my save data into databse – Rajendran bala Mar 02 '19 at 13:12
  • Sorry, you should edit your question and put the *relevant* code there. Also explain what you mean with "check multiple data": explain what you want to send to the client. See my answer, and what I wrote near the end. – trincot Mar 02 '19 at 13:16
  • I had updated my question and my code sir, please check my updated code – Rajendran bala Mar 02 '19 at 13:20
  • I have updated my answer. So understand this: put everything in the `async` function, also the check and the saving to the database. Don't put any code *after* the call to `find`, because that code would execute too soon. – trincot Mar 02 '19 at 13:26
  • Thank You sir I fixed my issue – Rajendran bala Mar 02 '19 at 13:43