0

According to the .native() documentation, the way to use .native() query for sails-mongo is :

Pet.native(function(err, collection) {
    if (err) return res.serverError(err);
    collection.find({}, {
        name: true
    }).toArray(function (err, results) {
          if (err) return res.serverError(err);
               return res.ok(results);
       });
});

How can I avoid callback here and use promises instead. Note that I have to use aggregate queries, so have to use .native()

jgr0
  • 697
  • 2
  • 6
  • 20
  • Have you had a look at [How do I convert an existing callback API to promises?](http://stackoverflow.com/q/22519784/1048572)? – Bergi Aug 30 '16 at 07:22

1 Answers1

2

As mentioned here Open bootstrap.js in config and monkey patch all methods with promise like this

module.exports.bootstrap = function(cb) {
var Promise = require('bluebird');

Object.keys(sails.models).forEach(function (key) {
    if (sails.models[key].query) {
        sails.models[key].query = Promise.promisify(sails.models[key].query);
    }
});

cb();  };

On the bonus side you get to use the latest version of bluebird with all models. Hope it helps

Community
  • 1
  • 1
user93
  • 1,866
  • 5
  • 26
  • 45