Monday 8 May 2017

mongoose aggregate callback only return [object], where is the data`?

This is my Model

const translateSchema = mongodb.Schema([{
  floor: {
    type: String
  },
  view: {
    type: String
  }
}]);

const TranslateModel = module.exports = mongodb.model('TranslateModel', translateSchema, 'translation');

module.exports.getTranslate = function(parms, callback){

  TranslateModel.aggregate([
      {
        $match:{$and:[{property:"ATL-D406"},{language:"gb"}]}
      },
      {
        $unwind:"$texts"
      },
      {
        $project:{"_id":0,"floor":"$texts.floor","view":"$texts.view"}
      }
    ],function(err, result) {
      if (err) return handleError(err);    
      console.log('Mongo Result: ' + result);
      console.log('Mongo Result: ' + result[0]);
      callback(result);
      // callback(result[0]);
      // callback();
  });

}

The aggregate works fine, but all I get in the console is

Mongo Result: [object Object]
Mongo Result: [object Object]

How do I get to see the data inside the [object Object]?

Why wont it fit inside my Schema?

And when I try to send it back using callback(result); I get this error:

Error: Uncaught, unspecified "error" event. ([object Object])

When I use getOne everything works fine, I get the result and can return it etc.. What am I not seeing?



via torbenrudgaard

No comments:

Post a Comment