1

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
4
  • Can you print your first console with stringify and check the value is coming or not console.log(JSON.stringify(quesObj))? Commented Oct 30, 2016 at 12:20
  • as you are overriding it and then assign it which is obviously getting undefined Commented Oct 30, 2016 at 12:29
  • Using stringify I got back an undefined Commented Oct 30, 2016 at 12:33
  • Well I have to initialize it before I can modify it, right? Commented Oct 30, 2016 at 12:34

3 Answers 3

1

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);
}
});
Sign up to request clarification or add additional context in comments.

6 Comments

This work, but only from within the call back, Is there anyway to set an object outside to result?
Yes, you can use promise or async module to get it
Okay I will research that a little more and let you know what happens
use waterfall method of async module. see my updated answer
Thank you sooo much You all made me realize that I need to learn async a little better. Can't tell you how many hours I put into this problem :)
|
1

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

3 Comments

So for the first answer my console was still null and undefined
The second answer with the call back: console was [object Object] and undefined
Try to log the object like this: console.log('and quesobj here: ', quesObj); with a comma instead of plus sign
1

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

7 Comments

That's still sending back a null and undefined
what is an output og console that is used in pushQues funtion
It returns [object Object]
make return in Question.findOne then when you calling pushQues after console RESULT what is ?
Inside the function if returns the correct function, but outside returns RESULT Promise{<pending>} . question here: undefined
|

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.