Insert value into db.json with help of angular-resource

I have a json file named db.json which is like

{
"menu":[
 {
   "id":0,
   "item":"item1"
},{
   "id":1,
   "item":"item2"
},{
   "id":2,
   "item":"item3"
}
],
"feedback":[]
}

I am using angular $resource to PUT an Javascirpt object into the feedback array.

Here is my service code-

this.getFeedbacks = function(){
            return $resource(baseURL + "feedback/:id", null, {
                'update': {
                    method: 'PUT'
                }
            });
        };

And here is the controller code-

$scope.fb.push($scope.myFeedback);
 menuFactory.getFeedbacks().update({
            id: $scope.fb.id
        }, $scope.fb);

Notice that the feedback array in the json is empty and the $scope.myFeedback is found from a ng-controller's ng-model and this ng-controller has a submit button when I click on the submit button then the values of the inputs are stored into the json file. The HTML code is-

<form name="feedbackForm" ng-submit="setFeedback()">
   <input type="text" name="name" ng-model="myFeedback.name">
   <input type="submit" name="submit" value="feedback">
</form>

My update function is not working why please someone explain it. Note that I am very beginner at angularJS platform and REST client-server networking.

Answers


I believe this is an exercise from Coursera. Here is my solution.

Instead of using the 'update', you should use 'save' method from $resource to add data into db,json file.

in your controller, say:

menuFactory.getFeedbacks().save($scope.feedback);

$scope.feedback should be the name you used in the feedback form object

Hope this answer help you!


First of all, watch browser console. It often gives you hints, where to look for the error. Coming back to the problem: try using $update instead of update.


Need Your Help

Volley JSONArrayRequest - not sending params properly?

java android post android-volley

I've tried with normal JSONArrayRequests and StringRequests and everything was fine untill now. I want to send an JSONArrayRequest with POST parameters to get some MySQL result in JSON format from ...