Reputation: 333
I'm looking to reverse and limit results. Reversing works fine, limiting works fine, but chained together, the results aren't how I expect. If limit is set to 10 I get 0 results. If it's set to 200 I get 6 results. Any idea what's causing this?
Users
.query(q => {
q.orderBy('id', 'DESC').limit(10)
})
.fetch({withRelated: ['groups']})
.then(function(users) {
util.filterUsers(users.map(function(user) {
var groups = user.related('groups');
return {
id: user.id,
handle: user.get('handle'),
githubid: user.get('githubid'),
name: user.get('name'),
public: user.get('public'),
url: user.get('url_hash'),
image: user.get('image'),
email: user.get('email'),
groups: groups.reduce(function(prev, group) {
prev[group.id] = group.get('group_name');
return prev;
}, {}),
};
}), req.user.id)
.then((results) => {
res.status(200).send(results);
});
Upvotes: 1
Views: 1026
Reputation: 5729
You need to add a return statement before your function util.filterUsers
. Try this
Users
.query(q => {
q.orderBy('id', 'DESC').limit(10);
})
.fetch({withRelated: ['groups']})
.then(function(users) {
return util.filterUsers(users.map(function(user) {
var groups = user.related('groups');
return {
id: user.id,
handle: user.get('handle'),
githubid: user.get('githubid'),
name: user.get('name'),
public: user.get('public'),
url: user.get('url_hash'),
image: user.get('image'),
email: user.get('email'),
groups: groups.reduce(function(prev, group) {
prev[group.id] = group.get('group_name');
return prev;
}, {})
};
}), req.user.id);
})
.then((results) => {
res.status(200).send(results);
});
Upvotes: 1
Reputation: 7815
Each then()
is expected to return a promise itself. If a common value is returned Bluebird (Bookshelf's choice for Promises) turns it into a promise itself.
In your case the then(function(users) {...})
is not returning anything, so it should be replaced by tap(function(users) {...})
. See tap on Bluebird documentation.
But IF util.filterUsers(...);
transforms the list somehow and it is a promise itself, you MUST return it, so placing its invocation under a return
statement: return util.filterUsers(...);
. In this case it is possible that it returns fewer users than the amount requested (that's what filters usually do...).
Upvotes: 2