0

I have a code like this.

var loadSchema = function(className) {
 var Schema = new schemaModel()

 schemaModel.findOne({className: className}), (err, schema) => {
  if (err) {
   throw err
  }

  if (!schema) {
   return 'schema not found'
  }
  var schemaOptions = schema.ob
  return schemaOptions
 })
}

Though I am returning result from a callback, the code is returning undefined data. In my thinking using callback we can initiate other works after the completion of one. I want to know how the callback work here.

Thanks.

Tolsee
  • 1,695
  • 14
  • 23
  • In your example the function `findOne` will return `schemaOptions` to the function `var loadSchema`. If you want to return the value `schemaOption` into `loadSchema` you need to do this: `var retval = schemaModel.findOne()...; return retval;` Then `var loadSchema` will be the returned `schemaOptions`, – DomeTune Mar 10 '17 at 13:59
  • Usually, `return` values from asynchronous callbacks are ignored in situations like that. Because the callback is happening at some point in time after the request was initiated, there's really no place for the return value to go. – Pointy Mar 10 '17 at 13:59
  • @DomeTune that is not correct. – Pointy Mar 10 '17 at 13:59
  • 1
    @Pointy i did not know that the function is async. Sorry for that. My comment only works on syncron functions. – DomeTune Mar 10 '17 at 14:04

0 Answers0