i have been googleing few weeks no real resolution.
i sure mark duplicate, not sure is, maybe being specific, anyway here goes.
i using angular in node-webkit app building. have api built in express , using mongodb (@mongolab) mongoose db.
i had working fine long of data types simple strings , numbers. had restructure data use arrays , complex objects. after restructuring data able post api calls work fine, cannot put calls work @ all.
the data looks this:
itemroles array, thought throwing error getting now, converted string.
itemstats causing problem. angular looking object, itemstats array (i think anyway). itemstats used string well, easier work in view if array of objects key:value pairs, why altered it.
i should note new mongodb well, first time using it.
{ "_id": { "$oid": "55a10b9c7bb9ac5832d88bd8" }, "itemroles": "healer,dps", "itemrating": 192, "itemname": "advanced resolve armoring 37", "itemclass": "consular", "itemlevel": 69, "itemstats": [ { "name": "endurance", "value": 104, "_id": { "$oid": "55a10b9c7bb9ac5832d88bda" } }, { "name": "willpower", "value": 124, "_id": { "$oid": "55a10b9c7bb9ac5832d88bd9" } } ], "__v": 0 } the mongoose schema looks this:
var mongoose = require('mongoose'); var schema = mongoose.schema; //var stats = new schema({ //name: string, //value: number //}); var armoringschema = new schema({ itemtype: string, itemclass: string, itemroles: string, itemlevel: number, itemname: string, itemrating: number, itemstats: [{ name:string, value:number}] }); module.exports = mongoose.model('armor', armoringschema); express api route:
/ on routes end in /armors/:id // ---------------------------------------------------- router.route('/armors/:id') // method omitted // update armoring specified id (accessed @ put http://localhost:8080/api/armors/:id) .put(function(req, res) { // use our armor model find armor want armoring.findbyid({_id: req.params.id}, function(err, armor) { if (err) { return res.send(err); } for(prop in req.body) { armor[prop] = req.body[prop]; } // save armor armor.save(function(err) { if (err) { return res.send(err); } res.json({success:true, message: 'armor updated!' }); }); }); }) resource factory:
swtorgear.factory('armoringfactory', ['$resource', function ($resource) { return $resource('http://localhost:8080/api/armors/:id', {}, { update: { method: 'put', params: {id: '@_id'}}, delete: { method: 'delete', headers: {'content-type': 'application/json'}, params: {id: '@_id'}} }); }]); route editing:
.when('/edit/armor/id/:id', { templateurl: 'views/modelviews/newarmor.html', controller: 'editarmorctrl', resolve: { armoring: ['$route', 'armoringfactory', function($route, armoringfactory){ return armoringfactory.get({ id: $route.current.params.id}).$promise; }] } }) contoller (just save method, first part of controller populates form existing data):
$scope.save = function(id) { $scope.armor.itemstats = [ $scope.armor.stats1, $scope.armor.stats2 ]; $scope.armor.itemroles = ''; if($scope.armor.role.tank) { $scope.armor.itemroles += 'tank'; } if($scope.armor.role.healer) { if($scope.armor.itemroles != '') { $scope.armor.itemroles += ',healer'; } else { $scope.armor.itemroles += 'healer'; } } if($scope.armor.role.dps) { if($scope.armor.itemroles != '') { $scope.armor.itemroles += ',dps'; } else { $scope.armor.itemroles += 'dps'; } } console.log($scope.armor); $scope.armor.$update(id) .then(function(resp) { if(resp.success) { var message = resp.message; flash.create('success', message, 'item-success'); $scope.armors = armoringfactory.query(); } else { var message = resp.message; flash.create('success', message, 'item-success'); } }); } formatted data being sent via put method (from console.log($scope.armor) ):

error on save:

i haven't seen nesting schemas in way you're doing it. here's try (hard if sure, there's lot going on):
var armoringschema = new schema({ itemtype: string, itemclass: string, itemroles: string, itemlevel: number, itemname: string, itemrating: number, itemstats: [{ name: string, value: number }] }); also need pass in object $update instead of number. change $scope.armor.$update(id) $scope.armor.$update({id: id}).
Comments
Post a Comment