0

I'm trying to get the results after executing a query and store it in a variable called "model".

db.collection.findOne({object},function(err,docs){
     model["output"]= docs;
})

The above code stores model["output"] as "undefined". How do I get hold of this value?

Sorry there was not enough code.

So there are two files.

FILE1

dbStmtModel.insertRecords(collectionName, record).then(
function (results) {
    console.log("results",results);
    result = results;
 }, function (err){
      return err;
 });
  model[statement.output] = result;

FILE2

function insertRecords(operand1,operand2){

    var deferred = q.defer();
    db.collection(operand1).update(operand2,operand2,{upsert:true},function (err,docs) {
        if(err) {
            deferred.reject(err);
        }
        else {
            deferred.resolve(docs);
        }
    });
    return deferred.promise

}

So tried using promises, tried using async. Still do not seem to get the model store the output of the result of the query. Also, there are no errors, since the callback returns correctly and prints the results. I'm not sure I'm using promises correctly though, although the console statement seems to print the results correctly. Also I'm using mongojs and not mongoose over mongodb(since the schema here is dynamic), don't know if thats going to change anything.

Community
  • 1
  • 1
zephyr
  • 1
  • 1
  • 1) There might be an error, you should check the `err` argument. 2) Make sure, you understand asynchronism. – qqilihq Aug 26 '16 at 19:06
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – dvlsg Aug 26 '16 at 19:10

3 Answers3

0

First of all, check for the err param you're ignoring

There is no enough code to be sure, but I'm guessing you defined model before in the same scope you're calling findOne and just below that you try to use model['output'].

This won't work since findOne (as almost every mongodb driver method) is asynchronous, so it's unlikely for its callback to be called before you try to use model['output'].

There's no quick solution for your problem, you need to understand asynchronism. Checkout this answer.

martriay
  • 5,632
  • 3
  • 29
  • 39
0
db.collection.findOne({object},function(err,docs){
  model["output"]= docs;
})

First - ill assume that your "model" object is defined. (It would throw that model is undefined, not model output) Second, You're not checking for error, if there was error docs end up empty. If still you've got Undefinded. There could be also error with model object. For instance - check if its even an object.

db.collection.findOne({object}, (err, docs) => {
   if(err) {
      return console.log(err)
   }
   model.output = docs;
}

Also! I'm just guessing but maybe you're trying to use it out of .findOne scope? What I mean - it is asynchronous call. So if you do something like this

db.collection.findOne({object}, (err, docs) => {
   if(err) {
      return console.log(err)
   }
   model.output = docs;
}
console.log(model.output);

then your model.output is undefined cause you call it before database returns data - it does not wait. You'll have to use callback (or promise) then.

callDB (object, cb) => {
   db.collection.findOne(object, (err, docs) => {
     if(err) {
        return (err)
      }
      return (null, docs);

    }
}

then you could call it

callDB({object}, (err, result) => {
   model.result = result;
});

But be advised that your new call for function is still asynchronous. So still your model.result will work only inside of its scope.

// I've seen you've updated your question, but I'll leave it here.

kWeglinski
  • 411
  • 4
  • 14
-1

First be sure that you are getting your results make a

console.log("RESULT",docs) if you getting your results then try below methods 

As mongo query return doc which is a model schema that can not be modify. Try this with lean it return document JSON object

var model={};
db.collection.findOne({object},function(err,docs){
     model.output= docs;
}).lean(true);

OR

var result={}
db.collection.findOne({object},function(err,docs){
result.model.output = docs;
})
Wasiq Muhammad
  • 3,080
  • 3
  • 16
  • 29