0

I have a function like this, which should return a schema after query is complete. I am not understanding why this function is always returning undefined value.

var loadSchema = function(className) {
 var query = schemaModel.findOne({className: className})
 var promise = query.exec()

 promise.then(function(schema){
  console.log(schema)
  return schema
 }).then(null, console.log)
}

I also tried to return a whole promise from function but still I cannot get the value. Like this

var loadSchema = function(className) {
 var query = schemaModel.findOne({className: className})
 var promise = query.exec()

 return promise.then(function(schema){
  console.log(schema)
  return schema
 }).then(null, console.log)
}

Can Anyone please tell me the flow of this program. I have watched other similar questions in stackoverflow but I cannot get the whole idea behind this.

Edit: This is the code where I am using this value.

module.exports.getSchema = function(req) {
 var className  = req.params.classname
 var schema = loadSchema(className)
 console.log(schema)
 return schema
}
Tolsee
  • 1,695
  • 14
  • 23
  • 1
    The first example returns `undefined` because you're not returning anything. Your second example is correct, but I don't know what "still I cannot get the value" means. Where is the code where you're trying to use the value? – JLRishe Mar 13 '17 at 06:42

1 Answers1

1

Your return statement is returning out of the function you supplied as a parameter for the .then.

One way around this is to supply a callback function to your loadSchema function that executes within the body of your then function, like so:

var loadSchema = function(className, callback) {
    var query = schemaModel.findOne({className: className});
    var promise = query.exec();

    promise.then(function(schema) {
        console.log(schema);
        callback(schema);
    }).then(null, console.log);
}

module.exports.getSchema = function(req, callback) {
    var className  = req.params.classname
    loadSchema(className, callback)
}

theModule.getSchema("req", function(schema) {
    //do stuff
});

You could also have your function return the promise, and then chain .thens after your loadSchema call. I am not sure what your schemaModel variable is, but it would be something along the lines of...

var loadSchema = function(className, callback) {
    var query = schemaModel.findOne({className: className});
    var promise = query.exec();

    return promise;
}

module.exports.getSchema = function(req, callback) {
    var className  = req.params.classname
    return loadSchema(className, callback)
}

theModule.getSchema("aClassName").then(...)...

See here for an example of the above statement with a manually generated promise: http://plnkr.co/edit/dVRs5cmErPp0JCd5CQPU

ryanlutgen
  • 2,951
  • 1
  • 21
  • 31
  • Thanks for the answer. Using your second approach, If I have a chain of functions like I showed in Edit, Should I return promises from one function to others? I am new to promises, so thanks again. – Tolsee Mar 13 '17 at 06:52
  • You can apply the same thing in your module.exports, either return loadSchema(...), and then the consumer would need to do theModule.getSchema(...).then(...) since you do not depend on another promise within the module.exports function, or you can the same callback approach. I have edited my answer to reflect these approaches. See here for more info on general promises: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise – ryanlutgen Mar 13 '17 at 06:54