1

I have used mongoose and Node.js and I am facing this issue:

var dbvar;
doc_model
  .findOne({aadhar})
  .then(()=>{
    dbvar=1;
  });
paramedic_model
  .findOne({aadhar})
  .then(()=>{
    dbvar=2;
  });
console.log(dbvar);

There's 2 collection in my MongoDB and I want to change the variable dbvar accordingly. But when it reaches to console.log(dbvar), it shows me undefined.

Wasif Ali
  • 886
  • 1
  • 13
  • 29
Yash Choksi
  • 2,132
  • 3
  • 9
  • 18
  • Try not to declare your dbvar multiple times, declare it once globally and then use. – Wasif Ali Sep 26 '18 at 06:37
  • @WasifAle Still it's not working after what you mentioned! – Yash Choksi Sep 26 '18 at 06:37
  • 1
    Because you are using asynchronous calls that's why you're not getting the desired output. try to console after calls completion. – Wasif Ali Sep 26 '18 at 06:43
  • may this is not the right approach, but you can do this by promise chaining `var dbvar = []; doc_model .findOne({ aadhar }) .then(() => { dbvar.push(1); return; }) .then(() => { paramedic_model .findOne({ aadhar }) .then(() => { dbvar.push(2); return; }); }) .then(() => { console.log(dbvar); })` – Harry Sep 26 '18 at 07:55

4 Answers4

1

You are shadowing your variable dbvar, try to remove the variable dbvar declaration within your `.then()' functions.

The console.log should also be placed within `.then()' so you can see your variable being populated when connection is made (which I suppose in your case is async).

var dbvar;
  doc_model
    .findOne({aadhar})
    .then(()=>{
       dbvar=1;
       console.log(dbvar);
    });
  paramedic_model
    .findOne({aadhar})
    .then(()=>{
      dbvar=2;
      console.log(dbvar);
    });
GibboK
  • 71,848
  • 143
  • 435
  • 658
0

Yes. In JavaScript, declarations in functions do not leak outside.

Your problem is name shadowing: By declaring dbvar again inside your function, you shadow your dbvar outside. After declaring dbvarin your function, all accesses to that name dbvar go to that inner variable.

I think just deleting like this:

var dbvar;
...
.then(() => {
   //var dbvar; <-- delete this line
   dbvar=1; // <- can access outer dbvar
   console.log(dbvar); //Log HERE
...

Should work

On a different note: You have to access your result from inside your then-callback. You should look up the promises concept a bit more before continuing :)

Nigel Nop
  • 180
  • 1
  • 6
  • Any solution for this issue? – Yash Choksi Sep 26 '18 at 06:38
  • As per you mentioned I did something like this, still not working... var dbvar; doc_model .findOne({aadhar}) .then(()=>{ dbvar=1; }); paramedic_model .findOne({aadhar}) .then(()=>{ dbvar=2; }); console.log(dbvar); – Yash Choksi Sep 26 '18 at 06:40
  • 1
    Ok, I will see in that first. Thanks. – Yash Choksi Sep 26 '18 at 06:42
  • Okay, I edited again - the logging has to take place inside of the then-callback. I urge you to look into the asynchronous behaviour of these promises to avoid these issues in the future :) – Nigel Nop Sep 26 '18 at 06:43
0

If you want to see that dbvar is changing it's value.

Console inside your calls:

var dbvar;
doc_model
  .findOne({aadhar})
  .then(()=>{
     dbvar=1;
     console.log(dbvar);
  });
paramedic_model
  .findOne({aadhar})
  .then(()=>{
    dbvar=2;
    console.log(dbvar);
  });
Wasif Ali
  • 886
  • 1
  • 13
  • 29
0

You are doing some async calls. So js works like this, the call from your .findOne().then() might take some time(can take seconds) to complete but during this time JS will keep going through your code so it will get to the console.log(dbvar); which is the last line of your code. At this point the dbvar will be undefined because you declared it globally like this var dbvar with no value. After some time your .findOne() will complete so the dbvar will get a value of 1 and 2 but you already printed this before when it was undefined.

There is no problem with your code, you only need to put the console log inside your .then() calls

var dbvar;
doc_model
  .findOne({aadhar})
  .then(()=>{
    dbvar=1;
    console.log(dbvar);
});
paramedic_model
  .findOne({aadhar})
  .then(()=>{
    dbvar=2;
    console.log(dbvar);
});
Cata John
  • 1,371
  • 11
  • 19