angularjs - using mongolab with angular for update(put) but record is not getting updated correctly -
i have following angular code below. i'm noticing when call ng-click="update($index, list.name)" update name field, creating new key/value pair in json list id not necessary. other fields such type, cdn etc. getting wiped out. want update name field. thanks!
var tools = angular.module("tools", ['ngresource']) tools.config(function($routeprovider) { $routeprovider.when('/home', { templateurl: 'home.html', controller: 'homecontroller' }); $routeprovider.when('/about', { templateurl: 'about.html', controller: 'aboutcontroller' }); $routeprovider.otherwise({ redirectto: '/home' }) }); tools.controller("homecontroller", function($scope, fetchdata, containitems, fetchdata) { $scope.arrayofmodel = ["nothing"]; $scope.clearsearch = function() { $scope.search = ""; $scope.name2 = ""; } $scope.name2 = ""; $scope.search = ""; //read $scope.record = fetchdata.query(); //create $scope.addnew = function(name, $location) { //create forum object send back-end var forum = new fetchdata($scope.addnew1); //save forum object forum.$save(function(response) { $scope.record.unshift(response); //$scope.record = fetchdata.query(); }, function(response) { //post response objects view $scope.errors = response.data.errors; }); } //delete $scope.destroy = function(index) { //alert($scope.record[index]._id.$oid); //return false; //tell server remove object fetchdata.delete({ id: $scope.record[index]._id.$oid }, function() { //if successful, remove our collection $scope.record.splice(index, 1); }); } //update $scope.update = function(index, newname) { fetchdata.update({ id: $scope.record[index]._id.$oid, name: newname }, function() { console.log('posted'); $scope.record = fetchdata.query(); }); } }); tools.controller("aboutcontroller", function($scope) {}); tools.factory('fetchdata', function($resource) { return $resource('https://api.mongolab.com/api/1/databases/frameworks/collections/list/:id?s={name: 1}&apikey=_qns_m-iz9-rckjnmvyemvvayl', {}, { 'get': { method: 'get' }, 'save': { method: 'post' }, //create 'query': { method: 'get', isarray: true }, //read 'remove': { method: 'delete' }, 'update': { method: 'put', params: { id: "@id" } }, //update 'delete': { method: 'delete', params: { id: "@id" } } }) //delete });
also here view:
<tbody> <tr ng-repeat="list in record | filter: {type:name2, name: search}"> <td>{{list._id.$oid}}</td> <td><input ng-model="list.name"></td> <td>{{list.type}}</td> <td><img src="{{list.logo}}" /></td> <td><a target="_blank" href="{{list.url}}">url</a></td> <td><a ng-show="list.cdn != ''" href="{{list.cdn}}">cdn</a></td> <td><a target="_blank" class="btn btn-primary btn-large" href="{{list.download}}" ng-click="putconsole(list.name)">download</a></td> <td><a target="#" class="btn btn-primary btn-large" ng-click="destroy($index)">delete!</a></td> <td><a target="#" class="btn btn-primary btn-large" ng-click="update($index, list.name)">update!</a></td> </tr> <!--<tr><span ng-show="totalcount.length == '0'">no results found. please reset search , try again!</span></tr>-->
when perform update mongodb, replace existing document 1 specify unless use update operators:
http://docs.mongodb.org/v2.2/applications/update/
in case, if want change value of single field while leaving rest alone, have use "$set" operator:
http://docs.mongodb.org/v2.2/applications/update/#update-a-field-in-a-document
for example:
{ "$set" : { "name" : newname } }
this should appear in body of put request. _id of document want change should specified in url. see "view, update, or delete document" section in official api documentation more details:
https://support.mongolab.com/entries/20433053-rest-api-for-mongodb
Comments
Post a Comment