Sunday, 15 June 2014

angularjs - Document sub-arrays stored as duplicate values of the first entry in Mongo -



angularjs - Document sub-arrays stored as duplicate values of the first entry in Mongo -

i'm using angular-fullstack , mongolabs when attempting update document sub-arrays values stored in mongolabs appear duplicate values of first element in array.

the mongoose schema set

techspecs: [{label: string, value: string, _id: false}],

when updating payload appears right , contains different values e.g.

techspecs: [{label: "dimensions", value: "1200x800mm"}, {label:"capacity", value: "532 l"}]

however in mongolabs values stored

"techspecs": [ { "label": "dimensions", "value": "1200x800mm" }, { "label": "dimensions", "value": "1200x800mm" } ],

if have more value pairs keeps storing multiple duplicates of first element.

the update done using $resource through mill service

angular.module('prototypemeanapp') .factory('productapi', ['$resource', function($resource) { homecoming $resource('api/products/:id', {id: '@_id'}, { 'update': { method:'put'}, 'create': { method:'post'}, 'query' : {method:'get', isarray:true} }); } ]);

in controller it's beingness updated via next function

$scope.updateproduct = function(form) { $scope.submitted = true; var pid = $scope.product._id; if(form.$valid) { productapi.update({id: pid}, { _id: pid, categoryid: $scope.currentcat[0]._id, brand: $scope.product.brand, model: $scope.product.model, heading: $scope.product.heading, description: $scope.product.description, price: $scope.product.price, techspecs: $scope.product.techspecs, lastmodified: date.now() }).$promise.then(function() { console.log('product updated'); }, function(err) { err = err.data; $scope.errors = {}; angular.foreach(err.errors, function(error, field) { form[field].$setvalidity('mongoose', false); $scope.errors[field] = error.message; }); });

and in view

<div class="form-group" data-ng-repeat="techspec in product.techspecs"> <label class="control-label hidden-xs col-sm-3">tech spec #{{$index+1}}</label> <div class="col-sm-4 col-md-3"> <div class="controls"> <input type="text" data-ng-model="techspec.label" class="form-control" placeholder="label" /> </div> </div> <div class="col-sm-4 col-md-3"> <div class="controls"> <input data-ng-model="techspec.value" type="text" class="form-control" placeholder="value" /> </div> </div> </div>

i ended simple test mean.io verify possible , mean.io stored document sub-arrays mongo. after compared source , implementation methods angular-fullstack , mean.io see different.

in end turned out lodash method used in update function.

angular-fullstack used _.merge whilst mean.io used _.extend after changing angular-fullstack code utilize _.extend sub-arrays correctly stored mongodb

for else using angular-fullstack. edit server endpoint controller e.g. server/api/products/product.controller.js

then find function update , replace _.merge _.extend e.g.

exports.update = function(req, res) { if(req.body._id) { delete req.body._id; } product.findbyid(req.params.id, function (err, product) { if (err) { homecoming handleerror(res, err); } if(!product) { homecoming res.send(404); } var updated = _.extend(product, req.body); updated.save(function (err) { if (err) { homecoming handleerror(res, err); } homecoming res.json(200, product); }); }); };

arrays angularjs mongodb mongoose

No comments:

Post a Comment