0
 let visitor;
 const role = req.user.profile;

 // role will be either Admin or Manager

 switch (role) {
   case 'Admin':
     if (req.body.verificationMode === 'online') {
       visitor = {
         name: req.body.name,
         mode: 'online',
       };
     } else {
       visitor = {
         name: req.body.name,
         mode: 'offline'
       };
     }
     break;


   case 'Manager':
     Staff.findOne({
       where: {
         loginId: req.user.id,
       },
     }).then((staffData) => {
       if (req.body.verificationMode === 'online') {
         visitor = {
           name: req.body.name,
           mode: 'online',
         };
       } else {
         visitor = {
           name: req.body.name,
           mode: 'offline',
         };
       }
     });
     break;

 }
 console.log('visitor >>> >> >> >> >>> >>>> >>>> >>>> >>> >>>> >>>', visitor);

If i login as Admin role, console of visitor having correct data, but if loginned as Manager role, console of visitor become undefined.

Why this is happening? I am using ES6, so i dont use var.

Mohamed Sameer
  • 2,998
  • 3
  • 22
  • 51
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Ivar Sep 27 '18 at 07:24
  • 1
    you have async code in case of `Manager` which executes after `console.log`. that why visitor is `undefined` in that case. – Aagam Jain Sep 27 '18 at 07:25
  • @AagamJain is right, your console.log is printed even before the visitor variable is set by the Staff.findOne async statement. – Ashwin Valento Sep 27 '18 at 07:28
  • 1
    @MohamedSameer The `.then()` in your code is executed **after** your `console.log()`. So visitor is not set yet. – Ivar Sep 27 '18 at 07:28
  • 1
    @Frost no, `const` and `let` or even ES6 would have absolutely no impact on async code. – VLAZ Sep 27 '18 at 07:32

1 Answers1

1

you have async code in case of Manager which executes after console.log. that why visitor is undefined in that case.

use this.

 let visitor;
 const role = req.user.profile;
 let callback = function(){
     console.log('visitor >>> >> >> >> >>> >>>> >>>> >>>> >>> >>>> >>>', visitor); 
 }
 // role will be either Admin or Manager



 switch (role) {
   case 'Admin':
     if (req.body.verificationMode === 'online') {
       visitor = {
         name: req.body.name,
         mode: 'online',
       };
     } else {
       visitor = {
         name: req.body.name,
         mode: 'offline'
       };
     }
     callback();
     break;


   case 'Manager':
     Staff.findOne({
       where: {
         loginId: req.user.id,
       },
     }).then((staffData) => {
       if (req.body.verificationMode === 'online') {
         visitor = {
           name: req.body.name,
           mode: 'online',
         };
       } else {
         visitor = {
           name: req.body.name,
           mode: 'offline',
         };
       }
       callback();
     });
     break;

 }

moved console.log into a function which is called after visitor value initialised.

Aagam Jain
  • 1,546
  • 1
  • 10
  • 18