Firdous Firdous - 4 months ago 6
Node.js Question

How to write multiple remote methods in a model?

May be its a little tricky or I am too dumb to figure out. Every documentation and example illustrates how to write a remote method, but not multiple. In my case I have already written a remote method and its working perfectly.

module.exports = function(customer) {
customer.signup = function(data, cb) {
customer.create(data, function(err, response){
cb(err, response);
});
};

customer.remoteMethod(
'signup',
{
accepts: [{arg: 'data', type: 'object', http: { source: 'body' }}],
returns: {type: 'object', root: true}
}
);
};


When I try to add another remote method, its not working as required, may be due to some syntax error.

module.exports = function(customer) {
customer.signup = function(data, cb) {
customer.create(data, function(err, response){
cb(err, response);
});
};

customer.remoteMethod(
'signup',
{
accepts: [{arg: 'data', type: 'object', http: { source: 'body' }}],
returns: {type: 'object', root: true}
}
);

customer.resetPassword = function (data, cb) {
console.log(data);
cb(null, data);
};
customer.remoteMethod(
'resetPassword',
{
accepts: [{arg: 'data', type: 'object', http: { source: 'body' }}],
returns: {type: 'object', root: true}
}
);
};


I even tried some variations as to merge the remoteMethods declaration in an array etc, but none would work. Please suggest where I am not right.

Answer

You need to specify the path for them.

customer.signup = function(data, cb) {
        customer.create(data, function(err, response){
            cb(err, response);    
        });
    };

    customer.remoteMethod(
        'signup',
        {
        accepts: [{arg: 'data', type: 'object', http: { source: 'body' }}],
        returns: {type: 'object', root: true},
        http: {
                path: "/signup",
                verb: 'post',
                status: 201
            }
        }
    );

    customer.resetPassword = function (data, cb) {
      console.log(data);
      cb(null, data);
    };
    customer.remoteMethod(
        'resetPassword',
        {
        accepts: [{arg: 'data', type: 'object', http: { source: 'body' }}],
        returns: {type: 'object', root: true},
        http: {
                path: "/reset-password",
                verb: 'post',
                status: 201
            }
        }
    );
Comments