1

Hello I need to find/update users from a mongodb collections via angular. And I need to find them by _id and by username, so I created a service like this:

// Users service used for communicating with the users REST endpoint
angular.module('users').factory('Users', ['$resource',
    function($resource) {
        return $resource('users/:id', {}, {
            update: {
                method: 'PUT'
            }
        });
    }
]);

And on Express I have the relative API route:

app.route('/users/:userId').put(users.update);

Now, suppose I have another express route like this to check username availability:

app.route('/users/:username').get(users.check);

How can I integrate this last one in the same angular service?

UPDATE: Solved with this, is it right?

angular.module('users').factory('Users', ['$resource',
    function($resource) {
        return {
            byId: $resource('users/:id', {}, {
                update: {
                    method: 'PUT'
                }
            }),
            byUsername: $resource('users/:username', {}, {
            })
        };
    }
]);
1
  • How do you differentiate it on express side, both routes look same to me. Is the second route always used to check availability. Commented Sep 19, 2014 at 12:02

1 Answer 1

3

Do you want to do something like this?

Angular service:

angular.module('users').factory('Users', function($resource) {
var resource = $resource('users/:byAttr/:id', {}, {
    update: {
        method: 'PUT',
        isArray: false,
        cache: false
    }
});
return {
    updateById: function (id) {
        resource.update({id: id, byAttr: 'id'});
    },
    updateByName: function (username) {
        resource.update({username: username, byAttr: 'username'});
    },
}

});

Routes:

app.route('/users/id/:userId').put(users.update);
app.route('/users/user/:username').get(users.check);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.