0

I'm using node.js and monogdb for my current project. I'm having problem that I tried to solve but no hope. So, I'm need for your help. I have problem with passing the result of the Mongo query callback to the route in main.js. In mongoDB.js class I have this function:

DBManagerConnection.prototype.findSubscriber = function(id){
   database.Subscriber.findOne({subscriberId:id}, function(err, subscriber){
      if(err || !subscriberid){ 
        console.log("No Subscriber recoard here");
      }else{
        console.log("Find:");
        console.log(subscriber);
        return subscriber;
      }
   });
 }

Output of this function is:

Find:
{ _id: 53a597665f8930ed1665c612,
  subscriberId: 'Sub8efc871fc6fc43765b2c9',
  subscriberName: 'Sub1',
  subscriberDevices: [],
  subscriberIndex: [],
  date: Sat Jun 21 2014 10:32:06 GMT-0400 (EDT) }

So far so good but when I call this function for main.js it returns 'undefined' as below:

var subb = null;
subb = db.findSubscriber('CPNsSub8efc871fc6fc43765b2c9');
setTimeout(function(){console.log(subb)}, 1000);

Output is undefined.

Can anyone help me please? Thanks

2 Answers 2

2

Your findSubscriber function does not return anything, so it is being set to undefined.

I'll try to make that clearer:

DBManagerConnection.prototype.findSubscriber = function(id){

  database.Subscriber.findOne({subscriberId:id}, function(err, subscriber){
    if(err || !subscriberid){ 
      console.log("No Subscriber recoard here");
    } else{
      console.log("Find:");
      console.log(subscriber);
      return subscriber;
    }
  });

};

Your return line is inside of the function(err, subscriber){ function, not inside of the function(id){ function. So in this case, when you do the return subscriber;, you are returning that value into the logic of .findOne, not to the call-site of .findSubscriber.

Generally what you are trying to accomplish would be done with another callback:

DBManagerConnection.prototype.findSubscriber = function(id, callback){
  database.Subscriber.findOne({subscriberId:id}, function(err, subscriber){
    if(err || !subscriber){ 
      console.log("No Subscriber record here");

      callback(err, null);
    } else{
      callback(null, subscriber);
    }
  });
};

and

db.findSubscriber('CPNsSub8efc871fc6fc43765b2c9', function(err, subscriber){
  console.log(err, subscriber);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Clear and sample. Now I really need to read more about callback functions. It seems I do not understand it. Thanks alot bro for your help and time.
0

Passing back value in async call using "return" keyword won't work. You need to pass it back with callback. Here is the code:

DBManagerConnection.prototype.findSubscriber = function(id, callback){
   database.Subscriber.findOne({subscriberId:id}, function(err, subscriber){
     if(err || !subscriberid){ 
       console.log("No Subscriber recoard here");
       callback({message: "Not found"});
     }else{
       console.log("Find:");
       console.log(subscriber);
       callback(null, subscriber);
     }
   });
};


db.findSubscriber('CPNsSub8efc871fc6fc43765b2c9', funciton(err, result) {     
  if (err) {
    console.log(err);
  } else {
    console.log(result);
  }
});

1 Comment

Thanks alot bro for your help and time but I'm going with last answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.