-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Breaking bug: v3.6.12 breaks promise-style queries (no callbacks) #1542
Comments
Why are you executing a count without a callback? |
It's not that there isn't a callback, it's that it's not supplied immediately at the point where the query is created. In this case, both the information from a count and from querying itself are necessary (say, for pagination purposes). So, two queries are made, and then the promises are "combined". For example, like this: var api = module.exports = {}
api.widgets = {
page: function (req, res) {
var page = Math.max(req.params.page - 1, 0) || 0 // reindex from 0, api is 1-indexed
var countP = Widgets.find().count().exec();
var widgetsP = Widgets.find()
.sort('order')
.skip(page * PAGE_SIZE)
.limit(PAGE_SIZE)
.exec();
Q.all([widgetsP, countP]).spread(function(widgets, count) {
res.json({
prevCount: countPieces(count, page - 1)
nextCount: countPieces(count, page + 1)
widgets: widgets
});
});
}
}
var countPieces = function(count, pageIdx) {
if (page < 0) { return 0; }
return Math.max(0, Math.min(PAGE_SIZE, total - (page * PAGE_SIZE)));
} The code sample may not be perfect, it's just an attempt to remember off the top of my head a simple use case. In any case, I have often found that one query is not sufficient to retrieve all of the information needed, and this style is far more succinct than chaining queries. It also means I can condense error handling... |
Ohhhh ok. Thanks, we'll get a fix out soon. On Thursday, June 27, 2013, Einhorn Media Group wrote:
Aaron |
Awesome, thanks 👍 |
Queries such as the one that follows no longer work:
Here is a trace for an example of this failure (forgive the Heroku timestamps).
I'm not entirely sure what causes this bug, but it seems to be a problem with the mechanism by which Mongoose handles passing callbacks to the native driver when one doesn't exist. The code for
exec()
shows that it seemingly just resolves the promise when an op isn't passed, which also seems odd. I'd like to look into this more, but I'm lacking in time.If there's anything further anyone would like me to check, please let me know.
In my testing I found this only to be the case on 3.6.12, 3.6.11 works perfectly fine.
The text was updated successfully, but these errors were encountered: