我正在关注http://mongoosejs.com/docs/populate.html以了解如何填充我拥有的引用数组。但是,当我尝试在填充的数组上执行HTTP Get请求时,出现错误
Error! { [CastError: Cast to ObjectId failed for value "blah" at path "_id"]
message: 'Cast to ObjectId failed for value "blah" at path "_id"',
name: 'CastError',
type: 'ObjectId',
value: 'blah',
path: '_id' }
blah
是数组中的值。以下是我的尝试:
HTTP Get call:
$scope.user = Auth.getCurrentUser();
$http.get('/api/users/' + $scope.user._id)
.success(function(data, status, headers, config){
$scope.currentUser = data;
});
用户架构:
var UserSchema = new Schema({
name: String,
username: String,
eventsAttending: [{ type: String, ref: 'Event'}],
});
和HTTP GET功能:
router.get('/:id', controller.getEvents);
exports.getEvents = function(req, res) {
User.findById(req.params.id).populate('eventsAttending').exec(function(err, events){
if(err) {
console.log("Error!", err);
return res.send(500, err);
}
return res.json(200, events);
});
};
老实说,我不太清楚为什么我的数组中的值被转换为Object Ids。我明确指出数组中的内容是字符串,而不是对象ID,它将引用事件模式。我知道我的数组里面的内容很简单。知道为什么会这样吗?
编辑:我发布了一个示例对象。在这里是我要填充的eventsAttending数组:
{
"name" : "Jane Doe",
"username" : "jane93",
"eventsAttending" : [
"blah",
"TestEvent",
"Asdf",
"TestEvent2"
]
}