1

I have an app which posts, gets and deletes data and I would like to add 'update' functionality as well but I can't figure it out.. I have a node.js server which has such api:

app.get('/api/feedbacks', function(req, res) {

    // use mongoose to get all feedbacks in the database
    getfeedbacks(res);
});

// create feedback and send back all feedback after creation
app.post('/api/feedbacks', function(req, res) {
    // create a feedback, information comes from AJAX request from Angular
    FeedBack.create(req.body, function(err, feedback) {
        if (err)
            res.send(err);

        // get and return all the feedbacks after you create another
        getfeedbacks(res);
    });

});

// delete a feedback
app.delete('/api/feedbacks/:feedback_id', function(req, res) {
    FeedBack.remove({
        _id : req.params.feedback_id
    }, function(err, feedback) {
        if (err)
            res.send(err);

        getfeedbacks(res);
    });
});

and such angular service which speaks to node api:

service.factory('FeedBacks', ['$http',function($http) {
    return {
        create : function(feedBackData) {
            return $http.post('/api/feedbacks', feedBackData);
        },
        get : function() {
            return $http.get('/api/feedbacks');
        },
        delete : function(id) {
            return $http.delete('/api/feedbacks/' + id);
        } 
    }
}]);

That way I can post, get and delete data.

My goal is to add also update function.

What I have tried on node:

// update a feedback
app.put('/api/feedbacks/:feedback_id', function(req, res) {
    // edit a feedback, information comes from AJAX request from Angular
    FeedBack.put(req.body, function(err, feedback) {
        if (err)
            res.send(err);

        // get and return all the feedbacks after you edit one
        getfeedbacks(res);
    });
});

on Angular service:

update: function(editFeedId, editedFeed){
            return $http.put('/api/feedbacks/' + editFeedId, editedFeed);
}

controller looks like:

$scope.editFeed = function(id) {
    $scope.editFeedId = id;
    $scope.editedFeed = 'replace this txt'


    FeedBacks.update($scope.editFeedId, $scope.editedFeed)
        // if successful creation, call our get function to get all the new
 feedBacks
        .success(function(data) {
            console.log('updated');
             $scope.feedbacks = data; 
        });
};

I get 500 error as I execute editFeed(). I couldn't figure out to configure that! Where do I do wrong? Any Tips?

Thanks a lot in advance!

2
  • You're using express on the Node.js server, right? If yes, are you using express body-parser? If you're not using it, your req.body will return null and it will not save the data. That's why the status 500. Commented Jan 16, 2015 at 13:57
  • 1
    @AndreyLuiz yes, I use express and I use body-parser.. Commented Jan 16, 2015 at 16:26

1 Answer 1

2

I'm assuming you're using Mongo here, in which case your update statement is incorrect.

It should be something like:

app.put('/api/feedbacks/:feedback_id', function(req, res) {
      FeedBack.update({_id: req.params.feedback_id}, req.body, function(err, feedback) {
        if (err)
            res.send(err);

        // get and return all the feedbacks after you edit one
        getfeedbacks(res);
    });
});
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.