Why is this nested relation in LoopBack returning duplicate results?
I was using this mixin that limits the limit
of a query to max out at a defined value. When include
is present in a query, the mixin also sets a limit on the scope of the include like so:
"include": {"foo":"bar","scope":{"limit":1}}
Seems the mixin was assuming all includes that are objects would be written in the form of {"relation":"foo", "scope":{"include:"bars"}}
, so includes were getting added twice.
For what it's worth, I wrote this simple mixin to limit the maximum number of results unless specified and stopped using the one linked above:
common/models/model.json:
"mixins": {
"ResultsetLimit": {
"limit": 100
}
}
common/mixins/resultset-limit.js:
const _ = require('lodash');
module.exports = (Model, options) => {
/**
* Modify incoming query to apply appropriate limit filters.
*/
Model.beforeRemote('**', (ctx, unused, next) => {
// Get the limit from the request, defaulting to the max limit.
const request_limit = _.toNumber(_.get(ctx, 'args.filter.limit', options.limit));
// Set the limit.
_.set(ctx, 'args.filter.limit', request_limit);
next();
});
};