简体   繁体   中英

MongoDB returns “undefined” node.js

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

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);
});

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);
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM