Bk Razor
Bk Razor

Reputation: 1532

Issue in returning plain json object from mongo

I have a function that finds one object inside my schema database. If I console.log it, it displays correctly. But outside the function when I use it or console log it, It returns a null object. Please help

My function:

var pushQues = function(quesObj){ 
var query = {AnswerId: 'Texas'};
Question.findOne(query).lean().exec(function(err,docs){
    console.log(docs.Ques);
    console.log(docs);
    quesObj = docs;
});};

Console(Returns Correctly):

Which is the biggest state?
{ _id: 5815366d49fd95ec160728d8,
 Ques: 'Which is the best language?',
 Answers: [ 'Texas', 'Georgia', 'New york', 'Ohio' ],
 AnswerId: 'Texas' }

Trying to retrieve quesObj from outside function:

var quesObj={};
pushQues(quesObj);
console.log('and quesobj here: '+ quesObj);
console.log('question here: '+ quesObj.Ques);

console(Doesnt work):

 and quesob here: [object Object]
 quesob here: undefined

Upvotes: 1

Views: 332

Answers (3)

abdulbari
abdulbari

Reputation: 6232

It should be the issue of async nature try to pass the callback to get it.

var pushQues = function(quesObj, callback) {
  var query = {
    AnswerId: 'Texas'
  };
  Question.findOne(query).lean().exec(function(err, docs) {
    console.log(docs.Ques);
    console.log(docs);
    callback(null, docs);
  });
};


pushQues(quesObj,function(err,result){
console.log('and quesobj here: '+ result);
console.log('question here: '+ result.Ques);  
});

Update

var async = require('async');
async.waterfall([
  function(callback) {
    var pushQues = function(quesObj, callback) {
      var query = {
        AnswerId: 'Texas'
      };
      Question.findOne(query).lean().exec(function(err, docs) {
        console.log(docs.Ques);
        console.log(docs);
        callback(err, docs);
      });
    };
  }
], function(err, result) {
if(!err){
  console.log(result);
}
});

Upvotes: 1

Wasiq Muhammad
Wasiq Muhammad

Reputation: 3118

I am assuming you want this pushQues function declaration as it is an issue of async

var pushQues = function(quesObj, callback){ 
var query = {AnswerId: 'Texas'};
var quesObj = {} ;
return Question.findOne(query).lean().exec(function(err,docs){
    console.log(docs);
    quesObj = docs;
    callback(null, docs);
});};

calling pushQues Function

pushQues(quesObj,function(err,result){
console.log('and quesobj here: '+ result);
console.log('question here: '+ result.Ques);  

Upvotes: 1

lucas.coelho
lucas.coelho

Reputation: 924

The problem is that you are replacing the object reference. You can like this:

var quesObj={ result: null };
pushQues(quesObj);
console.log('and quesobj here: '+ quesObj);
console.log('question here: '+ quesObj.result.Ques);

The function:

var pushQues = function(quesObj){ 
  var query = {AnswerId: 'Texas'};
  Question.findOne(query).lean().exec(function(err,docs){
      console.log(docs.Ques);
      console.log(docs);
      quesObj.result = docs;
  });
};

Another option is to use callback:

var pushQues = function(callback){ 
  var query = {AnswerId: 'Texas'};
  Question.findOne(query).lean().exec(function(err,docs){
      console.log(docs.Ques);
      console.log(docs);
      callback(err, docs);
  });
};



var quesObj={ result: null };
pushQues(function(err, obj) {
  if (err) console.log(err);
  console.log('and quesobj here: '+ quesObj);
  console.log('question here: '+ quesObj.Ques);
});

Upvotes: 1

Related Questions