angularjs - Error for Cast to ObjectId failed for value "[object Object],[object Object],[object Object] -
i using mongodb . when want update fields getting following error cast objectid failed value
[object object],[object object],[object object]
.
when post empty let me update properties while post somethings not able update provider properties , getting following error view have used modal ui bootstapt.
<label class="col-lg-2 control-label">company name</label> <div class="col-lg-10"> <input name="name" type="text" ng-model="provider.name" minlength="4" class="form-control" placeholder="amazon web services" required> <span class="help-block" ng-show="cspregister.name.$dirty && cspregister.name.$invalid"> not valid name!</span> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">abbreviated company name</label> <div class="col-lg-10"> <input name="abbreviated" type="text" ng-model="provider.abbreviated" class="form-control" placeholder="aws" > </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">home page</label> <div class="col-lg-10"> <input name="url" type="url" ng-model="provider.url" class="form-control" placeholder="http://aws.amazon.com/" required> <span class="help-block" ng-show="cspregister.url.$error.url"> not valid url!</span> </div> </div> <!--start point of muli select--> <div class="form-group"> <label class="col-lg-2 control-label">product name</label> <div class="col-lg-10"> <select name="services" multiple ng-model="provider.services" class="form-control" required > <option value="paas">paas</option> <option value="saas">saas</option> <option value="dbaas">dbaas</option> </select> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">location</label> <div class="col-lg-10"> <select multiple ng-model="provider.locations" class="form-control" class="form-control" required> <option ng-repeat="item in locations" value="{{item.name}}">{{item.name}}</option> </select> <span class="help-block">a longer block of text breaks onto new line , may extend beyond 1 line.</span> </div> </div> <!--end of multi selector--> <div class="form-group"> <label class="col-lg-2 control-label">description</label> <div class="col-lg-10"> <textarea name="description" ng-model="provider.description" class="form-control" rows="3" required></textarea> </div> </div> </form> </div> <div class="modal-footer"> <button ng-click="updateprovider(provider);ok()" class="btn btn-success"><i class="icon-white icon-plus"></i> update & close</button> <button ng-click="cancel()" class="btn btn-warning">cancel</button> </div>
provider schema
var providerschema = new schema({ name: string, abbreviated: string, // company:string, services: {type : array, default:[]}, locations: {type : array, default:[]}, description: string, url: string, author: string, upvotes: { type:number, default:0 }, upvoteuser:{type : array, default:[]}, createdon: { type: date, default: date.now }, certificates:[{ type: mongoose.schema.types.objectid, ref: 'certificates' }], posts: [{ type: mongoose.schema.types.objectid, ref: 'post' }] }); providerschema.methods.findbyname = function (cb) { return this.model('provider').find({ name: this.name }, cb); } providerschema.methods.upvote = function(cb) { this.upvotes += 1; this.save(cb, function(error) { if(error){ console.error("error saving object: ", error) } }); };
this update in factory
ob.updateprovider = function (provider) { return $http.put('/api/providers/' + provider._id , provider, { headers: {authorization: 'bearer '+auth.gettoken()} }).success(function(provider){ }) };
and update function in api
exports.update = function(req, res) { if(req.body._id) { delete req.body._id; } provider.findbyid(req.params.id, function (err, provider) { if (err) { return handleerror(res, err); } if(!provider) { return res.send(404); } var updated = _.merge(provider, req.body); updated.save(function (err) { if (err) { return handleerror(res, err); } return res.json(200, provider); }); }); };
this have in controller.
$scope.updateprovider = function (updatedprovider) { var provider = updatedprovider; providers.updateprovider(provider) .success(function () { providers.getall(); $scope.status = 'updated provider! refreshing provider list.'; }) .error(function(err, status) { console.log(err); console.log(status); }); };
this result updated:
abbreviated: 'aws', _id: 55663fc20e9009c82dccba94, company: 'http://dbpedia.org/resource/snaplogic', name: 'snaplogic', description: 'snaplogic commercial software company provides data 2006. snaplogic headed ex-ceo , co-founder of informatica, gaurav dhill __v: 25, posts: [ { upvotes: 4, upvoteuser: [object], comments: [object], __v: 16, author: 'juan y maryam', body: 'cc', title: 'cczzs', _id: '5569f716a4c7bbd4219a3303' }, { upvotes: 4, upvoteuser: [object], comments: [], __v: 4, author: 'juan y maryam', body: 'zzaaa', title: 'juan', _id: '5569fb10c61b1af02fa1cc39' }, { upvotes: 4, upvoteuser: [object], comments: [], __v: 4, author: 'juan y maryam', body: 'xxxxx', title: 'xxxxxxxxxx', _id: '5569fb15c61b1af02fa1cc3a' }, { upvotes: 2, upvoteuser: [object], comments: [], __v: 2, author: 'test user', body: 'nnnnnnnnnnnnnnnn', title: 'ooooooooooooo', _id: '5569fcd3335d49d82ccef9cd' }, { upvotes: 1, upvoteuser: [object], comments: [], __v: 1, author: 'editor', body: 'ss', title: 'sssssss', _id: '556a07892d62dadc182fcc1f' }, { upvotes: 1, upvoteuser: [object], comments: [], __v: 1, author: 'maya beaty', body: 'ssssssss', title: 'ssssssss', _id: '556a07f92d62dadc182fcc23' }, { upvotes: 1, upvoteuser: [object], comments: [], __v: 1, author: 'maryam pashmi', body: 'z', title: 'z', _id: '556a13e62848566023f03e6d' }, 556a07f92d62dadc182fcc23, 556a13e62848566023f03e6d ], certificates: [], createdon: thu may 28 2015 00:05:54 gmt+0200 (romance daylight time), upvoteuser: [ 'maya beaty', 'maryam pashmi', 'juan y maryam' ], upvotes: 3, locations: [ 'Ă…land islands', 'albania' ], services: [ 'saas' ] } put /api/providers/55663fc20e9009c82dccba94 500 10ms
is there 1 know why getting error , issue have change in schema?
i don't have solution can try explain problem is, or @ least why isn't working in case. although there's couple of solutions @ end may or may not work..
your provider.posts
array of set of documents referenced "posts", defined in provider schema.
posts: [{ type: mongoose.schema.types.objectid, ref: 'post' }]
posts: [ {upvotes: 4, ... title: 'cczzs', _id: '5569f716a4c7bbd4219a3303' }, {upvotes: 4, ... title: 'juan', _id: '5569fb10c61b1af02fa1cc39' }, {upvotes: 4, ... _id: '5569fb15c61b1af02fa1cc3a' }, ... {upvotes: 1, _id: '556a13e62848566023f03e6d' }, 556a07f92d62dadc182fcc23, 556a13e62848566023f03e6d ],
these supposed saved ids (556a07…), because that's how they're defined in schema
actually if towards end there elements ids (556a07f92d62da…), ones fetched database, , others _.merge
d req.body
the problem when saving, these objects being converted string "[object object]" reason. isn't supposed happen or @ least doesn't happen me. have mongoose@3.8 installed, check if maybe you're on older version.
just make sure root cause of problem, should
delete req.body.posts; var updated = _.merge(provider, req.body); // updates.posts should have 2 ids mentioned above updated.save(...
and see if gets saved.
anyways going through older questions you'll find have had luck deleting , re-creating entire collection. advise it's bad idea store them objects in first place, in case can .map
these objects , convert them native string id or objectid before saving.
updated.posts = updated.posts.map(function(post){return post._id}); updated.save(...
hope helps out in way if doesn't solve it.
Comments
Post a Comment