我正在使用Mongodb。当我想要更新字段时,我收到以下错误Cast to ObjectId
失败的值:
[object Object],[object Object],[object Object]
。
当帖子为空时,让我更新所有属性,而有人发布的东西,然后我无法更新提供程序属性,我收到以下错误:
查看哪些使用了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 help text that breaks onto a new line and may extend beyond one 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>
提供商架构
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)
}
});
};
这是工厂中的更新
ob.updateProvider = function (provider) {
return $http.put('/api/providers/' + provider._id , provider, {
headers: {Authorization: 'Bearer '+Auth.getToken()}
}).success(function(provider){
})
};
这是我的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);
});
});
};
这就是我在控制器中所拥有的。
$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);
});
};
这是更新的结果:
abbreviated: 'aws',
_id: 55663fc20e9009c82dccba94,
company: 'http://dbpedia.org/resource/SnapLogic',
name: 'SnapLogic',
description: 'SnapLogic is a commercial software company that provides data an
2006. SnapLogic is headed by Ex-CEO and 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
有没有人知道我为什么会收到此错误以及我必须在架构中更改哪些问题?
答案 0 :(得分:2)
我没有解决方案,但我可以尝试解释问题是什么,或者至少为什么它不适用于您的情况。虽然最后有几个解决方案可能会或可能不起作用..
您的header
是由“帖子”引用的另一组文档的数组,如您的提供者架构中所定义。
provider.posts
posts: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Post' }]
这些实际上应该保存为ID(556a07 ...),因为它们是在你的Schema中定义的
实际上,如果你看到最后有一些元素实际上只是ID(556a07f92d62da ...),那些可能是从数据库中提取的元素,其他元素是posts: [
{upvotes: 4, ... title: 'cczzs', _id: '5569f716a4c7bbd4219a3303' },
{upvotes: 4, ... title: 'juan', _id: '5569fb10c61b1af02fa1cc39' },
{upvotes: 4, ... _id: '5569fb15c61b1af02fa1cc3a' },
...
{upvotes: 1, _id: '556a13e62848566023f03e6d' },
556a07f92d62dadc182fcc23,
556a13e62848566023f03e6d
],
来自_.merge
问题在于保存时,由于某种原因,这些对象正在转换为字符串“[Object object]”。这不应该发生,或者至少不会发生在我身上。我安装了mongoose@3.8,所以请检查一下你是否使用旧版本。
只是为了确保这正是问题的根本原因,你应该
req.body
并查看它是否已保存。
无论如何都要通过older questions,你会发现有些人幸运deleting and re-creating the entire collection。有些人建议它是bad idea to store them as objects in the first place,在这种情况下你可以delete req.body.posts;
var updated = _.merge(provider, req.body);
// now updates.posts should only have 2 IDs mentioned above
updated.save(...
这些对象,并在保存之前将它们转换为原生字符串ID或ObjectID。
.map
希望以某种方式帮助,即使它没有解决它。