0

I am relatively new to Javascript and I really don't understand what I am doing wrong here but the return value is not being set :

found = _dataDictionary.findOne(findByModelNameQuery, function(err, result) {
    if (result) {
        if (result.record) {
            found = true
        } else {
            found = false
        }
    }   
    return found
});
Misaz
  • 3,694
  • 2
  • 26
  • 42
  • 5
    You're probably using... asynchronous callback. –  Nov 12 '16 at 16:05
  • 1
    Did you call the function somewhere? – SBD Nov 12 '16 at 16:07
  • this is the 'complete' code I am using Model.observe('access', function(ctx, next, cb) { var _dataDictionary = loopback.findModel('dataDictionary'); found = _dataDictionary.findOne(findByModelNameQuery, function(err, result) { if (result) { if (result.record) { found = true } else { found = false } } return found }); ... set query.where based on the value found ..... ctx.query.where = query.where } next(); }); – Peter Mueller Nov 12 '16 at 16:11

2 Answers2

0

It appears that _dataDictionary.findOne is an asynchronous function. So the value returned from the statement is not the same as the one assigned later inside the callback.

Refactoring your example will hopefully show you this:

_dataDictionary.findOne(findByModelNameQuery, function(err, result) {
  if (result) {
    if (result.record) {
      found = true
    } else {
      found = false
    }
  }

  // This statement returns to the `findOne` line executing this callback 
  // return found;

  // Instead you can log here to see the value
  console.log('FOUND', found);
});

UPDATE: based on your comments (and a few more assumptions) you could refactor to provide the value back to the calling function:

Model.observe('access', function(ctx, next, cb) {
  var _dataDictionary = loopback.findModel('dataDictionary');

  _dataDictionary.findOne(findByModelNameQuery, function(err, result) {
    if (result) {
      if (result.record) {
        found = true;
      } else {
        found = false;
      }
    }

    // Assuming cb is the method to return values if async
    cb(found);
  });

  // Assuming this is for async behavior to notify to the `observe` caller that this is an async callback
  next();
});
Jason Cust
  • 10,743
  • 2
  • 33
  • 45
0

As you are using callback functions (the function in the last parameter in findOne), to return results, I sugest to you to use a callback function.

To get the result, you may do something like:

function myFunction(myParams, callback){
     _dataDictionary.findOne(myParams, function(err,result){
         return callback(err,result);
     });
}

Then you can call "myFunction" in other place like:

...
myFunction(params, function(err,result){
       //do something with the result)
}

OBS1: if the params are an another function, the "ugly" way to do this is using nested callbacks, that usually are a "antipattern".

    function myFunction(myParams, callback){
     _dataDictionary.findOne(myParams, function(err,result1){
         _anotherAsyncFunction(result1.params, function(err,result2){
               //do something with result2 and callback it.
        });
     });
}

OBS2: You can avoid this antipattern using libraries such async "waterfall" method or bluebird Promise Library "then" methods.