Search code examples
angularjsput

PUT Request with AngularJS and Express


When I'm performing a put request and console.log(response) of the request I only get a JSON Object like {"res":1} instead of getting the whole json object with its changes in order to update him in a database.

Controller :

$scope.doneEdit = function (components) {
        console.log(components);
        components.editing = false;
        if (components.editing === false) {
            $http.put('/propt/' + components._id).then(function (response) {
                console.log(response.data);
            });
        }
    }

Express

app.put('/propt/:id', function(req,res) {
    console.log(req.body);
    testDb.update({_id:req.params.id}, req.body, {}, function(err, numReplaced){
        res.statusCode = 200;
        res.send(req.body);
    })
})

Solution

  • You should pass the data you want to send as a second parameter to put method:

    $http.put('/propt/' + components._id, {someValue:components.someValue})
    

    You can find the documentation here: https://docs.angularjs.org/api/ng/service/$http#put