Hi, I'm using beforeRemote for "find" hook to add some extra stuffs to the filter from data extracted from query, something like this:
country_iso = ctx.req.query.country.toUpperCase();
This works fine, however in the explorer, it doesn't show that the user needs to pass "country" as a parameter, is there anyway i can change the "find" hook so that it will show country as a required parameter? Thank you.
Is country a property you defined in your model? Did you set it to required?
@0candy no it's not a property of the model, it's just a required param which i will use to process the "find" later. I just want to show it in the explorer so the mobile team knows what parameter they need to pass.
Hi, any updates so far?
@nmklong Hi, this is an interesting scenario. We don't have a first class support for it, but you may be able to describe your custom argument by modifying the remoting metadata of the find function.
Here is a mock-up to illustrate what I mean. I am afraid I don't have enough time to fully test it, it may not work OOTB.
MyModel.setup = function() {
MyModel.base.setup.apply(this, arguments);
var findMethod = this.sharedClass.find('find', true);
findMethod.accepts.push({
arg: 'country',
type: 'string',
required: true
});
};
Hmm, I am afraid the example above will not work, because the added argument will be sent to find method too, which will most likely break things, as find does not expect extra parameters.
I think the cleanest option is to stop using hooks and write a custom method instead. (Again, the code below may not work OOTB.)
MyModel.setup = function() {
MyModel.base.setup.apply(this, arguments);
this.disableRemoteMethod('find', true);
MyModel.findWithCountry = function(filter, country, cb) {
// update filter using country
return this.find(filter, cb);
};
MyModel.remoteMethod('findWithCountry', {
// copy metadata from loopback/lib/persisted-model.js
description: 'Find all instances of the model matched by filter from the data source.',
accessType: 'READ',
accepts: [
{ arg: 'filter', type: 'object', description:
'Filter defining fields, where, include, order, offset, and limit' },
// and add extra "country" arg
{ arg: 'country', type: 'string', description: '...' },
],
returns: { arg: 'data', type: [typeName], root: true },
http: { verb: 'get', path: '/' },
});
};
And make the 'country' arg required:
{ arg: 'country', type: 'string', description: '...' , required: true},
@bajtos Thanks a lot for the help, i'll go with this option!
I had the same requirement, and had to modify the snippet slightly to make it work
MyModel.setup = function() {
MyModel.disableRemoteMethodByName('find');
MyModel.findWithCountry = function(filter, country, cb) {
// update filter using country
return this.find(filter, cb);
};
MyModel.remoteMethod('findWithCountry', {
// copy metadata from loopback/lib/persisted-model.js
description: 'Find all instances of the model matched by filter from the data source.',
accessType: 'READ',
accepts: [
{ arg: 'filter', type: 'object', description:
'Filter defining fields, where, include, order, offset, and limit' },
// and add extra "country" arg
{ arg: 'country', type: 'string', description: '...' },
],
returns: { arg: 'data', type: [typeName], root: true },
http: { verb: 'get', path: '/' },
});
};
I had the same requirement, and had to modify the snippet slightly to make it work
MyModel.setup = function() { MyModel.disableRemoteMethodByName('find'); MyModel.findWithCountry = function(filter, country, cb) { // update filter using country return this.find(filter, cb); }; MyModel.remoteMethod('findWithCountry', { // copy metadata from loopback/lib/persisted-model.js description: 'Find all instances of the model matched by filter from the data source.', accessType: 'READ', accepts: [ { arg: 'filter', type: 'object', description: 'Filter defining fields, where, include, order, offset, and limit' }, // and add extra "country" arg { arg: 'country', type: 'string', description: '...' }, ], returns: { arg: 'data', type: [typeName], root: true }, http: { verb: 'get', path: '/' }, }); };
I tried this and didn't work for me. Where we should add this snippet?
I had the same requirement, and had to modify the snippet slightly to make it work
MyModel.setup = function() { MyModel.disableRemoteMethodByName('find'); MyModel.findWithCountry = function(filter, country, cb) { // update filter using country return this.find(filter, cb); }; MyModel.remoteMethod('findWithCountry', { // copy metadata from loopback/lib/persisted-model.js description: 'Find all instances of the model matched by filter from the data source.', accessType: 'READ', accepts: [ { arg: 'filter', type: 'object', description: 'Filter defining fields, where, include, order, offset, and limit' }, // and add extra "country" arg { arg: 'country', type: 'string', description: '...' }, ], returns: { arg: 'data', type: [typeName], root: true }, http: { verb: 'get', path: '/' }, }); };I tried this and didn't work for me. Where we should add this snippet?
I was able to solve it.
Most helpful comment
I had the same requirement, and had to modify the snippet slightly to make it work