How to add more remoteMethod to Built-in Models (say User) in Loopback?
I have created a common/models/user.js and added the following code
var loopback = require('loopback');
var User = loopback.User;
User.signup = function(userData, callback){
// Validate data
// Save data - User
// Create role mapping
// return token
}
User.remoteMethod(
'signup',
{
accepts: [{ arg: 'userData', type: 'object' }],
returns: { arg: 'token', type: 'object' },
http: { verb: 'post' }
});
But, signup doesn't show up in the explorer. Could you please help?
Cheers,
There is actually an open issue regarding how to handle this situation better. Right now it isn't pretty, but you can do it. The easiest way given your situation is probably going to be to create a simple boot script to extend User
.
In server/boot/
create a new file: a-new-user.js
(Unfortunately, the file must come alphabetically before explorer.js
otherwise the new remote method won't show up in the explorer interface.) Simply add the code below in that file and restart your application:
module.exports = function (app) {
var User = app.models.User;
User.signup = function(userData, callback){
// ...
callback(null, 'some response data');
};
User.remoteMethod(
'signup',
{
accepts: [{ arg: 'userData', type: 'object' }],
returns: { arg: 'token', type: 'object' },
http: { verb: 'post' }
}
);
};