Garuuk
Garuuk

Reputation: 2254

node express routing params not catching

I have this

    router.put('/user:resourceId', function(req, res) {

        User.findOneAndUpdate({resourceId: req.params.resourceId}, req.body, function(err, user) {
            if (err)
                res.send(err);

            res.send(user);
        });
    });

and in my angular code I have

    updateResource: function(resource){
        var self = this;
        return $http.put('api/resources/resource', resource, {params:{resourceId:resource.resourceId}}).then(function(response){
            return response.data;
        }, function(err){
            $log.error('Error updating', err);
        });
    }

Why isn't this path catching? It works when I remove the params.

Upvotes: 1

Views: 36

Answers (1)

Ash
Ash

Reputation: 6783

Your route looks a little off, try the following:

router.put('/user/:resourceId', function(req, res) {
  ...
});

For a URL that looks like: /user/123, where req.param.resourceId is 123.

It looks like your angular code is pointing to /api/resources/resource though, so I can understand why it's perhaps not matching your Express route for /user/:resourceId - (Unless your Express router is set to handle request for something like the following: /api/resources/resource/user/:resourceid?

Upvotes: 1

Related Questions