I was following http://mongoosejs.com/docs/populate.html to learn how to populate an array of references I had. However, when I try to perform an HTTP Get request on the populated array, I get an error that says
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' }
With blah
being the value inside the array. Below is my attempt:
HTTP Get call:
$scope.user = Auth.getCurrentUser();
$http.get('/api/users/' + $scope.user._id)
.success(function(data, status, headers, config){
$scope.currentUser = data;
});
User Schema:
var UserSchema = new Schema({
name: String,
username: String,
eventsAttending: [{ type: String, ref: 'Event'}],
});
and HTTP GET function:
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);
});
};
I honestly am not too sure why the values inside my array are being casted into Object Ids. I clearly specified that the contents in the array are Strings, not Object Id's which will be references to the the Event Schema. I know that the contents inside my array are simple Strings. Any idea why this is happening?
Edit: I am posting a sample object. In here lies the eventsAttending array I want to populate:
{
"name" : "Jane Doe",
"username" : "jane93",
"eventsAttending" : [
"blah",
"TestEvent",
"Asdf",
"TestEvent2"
]
}