node.js - Mongoose distinct with promises -
i use when.js promise library mongoose , try distinct filed of collection , return result promise
mongoose.promise= require("when"); function getpromiseddistinct(startdate, enddate) { return when.promise(function(resolve, reject) { $log.distinct("keys", { datetime: { "$gte": startdate, "$lt": enddate } }).exec() .then(function(res) { return resolve(res); }); }); }
however getpromiseddistinct resolved promise function
function (resolve, reject) { if (!_this.op) { callback && callback(null, undefined); resolve(); return; } _this[_this.op].call(_this, function(error, res) { if (error) { callback && callback(error); reject(error); return; } callback && callback.apply(null, arguments); resolve(res); }); }
edit #1 getpromiseddistinct called as
function agg(day, startdate, enddate) { return when.promise(function(resolve, reject) { getpromiseddistinct(startdate, enddate).then(function(keys) { log.data("keys : \n" + licence_keys); }).otherwise(function(keys) { return reject(err); }); }); }
why not resolved value?
you've instructed mongoose use when.js promise library. if works native promises (and seemingly bluebird , q) don't need wrap queries in promise, mongoose (once use exec()
) code written such:
function getpromiseddistinct(startdate, enddate) { return $log.distinct("keys", { datetime: { "$gte": startdate, "$lt": enddate } }).exec() }); }
you can same agg
function.
as @robertklep pointed out in comments there no need include then
block in getpromiseddistinct
because it's unnecessary clutter (see promise anti-patterns. returning promise object return $log.distinct...
, accessing value in agg
function.
it's worth taking note of mongoose's documentation on plugging in promises library:
mongoose tests es6 native promises, bluebird, , q. promise library exports es6-style promise constructor should work in theory, theory differs practice. if find bug, open issue on github
Comments
Post a Comment