I'm using nodeJS, running express4 framework to create an API. I am using RSVP.js for promises.
worker.getFamilies is called by an external module, not reflected here. The caller expects back an object with results.
Inside worker.getFamilies: - I am querying mongoDB using mongoose (works, we get results back)
I am using a promise to get the response object from mongoose when its available. That works.
Then I try to return JSON.stringify(results); to the caller of worker.getFamilies and that doesnt work. The caller gets back "undefined". please help.
var worker = exports; var RSVP = require('rsvp'); var getFamiliesFromDB = function(DBconnection){ var promise = new RSVP.Promise(function(resolve, reject){ DBconnection.find({} ,{limit:10, sort: [['_id',-1]]}).toArray(function(e, results){ console.log('results:', results); resolve(results); }); }); return promise; } worker.getFamilies = function(DBConnection){ getFamiliesFromDB(DBConnection).then(function(results){ //this works console.log('here are the fams from the promise', results); //this doesnt work like i want it to return JSON.stringify(results); }); };