Here is the outline of the main object:
var newsSchema = new Schema({ headline: String, paragraph: String, imgURI: String, imgThumbURI: String, imgCaption: String, addedOn: Date, addedBy: { type: ObjectID, ref: 'usr' } }); var News = mongoose.model('news', newsSchema);
... and the scheme for adding By:
var usr = new Schema({ username: String, avatar: { type: ObjectID, ref: 'avtr' }, href: String }); var UserModel = mongoose.model('usr', usr);
So far so good. Everything works. Then, in the Angular client, I retrieve the news object, but the value of the added value is not the desired object, but ObjectId:
{ "headline":"Shocking news from the Neverland!", ... "addedBy":"520e9aac9ca114914c000003", // <-- the offender!! "addedOn":"2013-08-16T21:33:32.294Z", "_id":"520e9aac9ca114914c000001", "__v":0 }
When I need an object like this:
{ "headline":"Shocking news from the Neverland!", ... "addedBy":{ "username":"Peter" "avatar":{ "src":"../images/users/avatars/avatar1.png", "ststus":"happy"} } "addedOn":"2013-08-16T21:33:32.294Z", "_id":"520e9aac9ca114914c000001", "__v":0 }
So, yes, I want all (without a mother) nested ObjectId to be replaced with the corresponding objects from the database before the main object is sent to the Angular client. The API that I create is deep and complex, and it would be nice if the Angular client could receive an object from my Express server that is ready to be thrown into the field.
How to change the following "/ news" route:
app.get('/news', function(req, res, next){ News. find(). exec(function(err, nws){ if(err) {res.writeHead(500, err.message)} res.send(nws); }); });
to accomplish this, so I can fully access the full (nested) object from Angular as follows:
angular.module('App', ['ngResource']) .controller('NewsCtrl', function($scope, $resource){ var News = $resource('/news'); var news = News.query(); $scope.news = news; });
and then on the api access website like this:
<img class="avatar-img" src="{{ news[0].addedBy.avatar.src }}">
I really appreciate your time, cheers Jared