Modifying mongoose object literal result does not work

773 views
Skip to first unread message

jurka

unread,
Feb 10, 2012, 7:54:10 AM2/10/12
to Mongoose Node.JS ORM
As I posted this question also to Stackoverflow, but only getting
views from there no answers i try to post here.
http://stackoverflow.com/questions/9226694/modifying-mongoose-object-literal-result-does-not-work

First I am making the query to mongoDB, get all the correct results
but only the small modification to object literal does not work. What
I am trying to do, is adding new field to comments. I tried to use the
DBref method but it didn't work so i make 2 queries now.

var query = Rss.findOne({ _id: itemId});
query.exec(function(err, feed) {
if (!err && feed.comments) {
console.log(feed.comments.length);
for (var index in feed.comments) {
var author = feed.comments[index].author;
if (author !== undefined) {
User.findById(author, function(err, user) {

/**Problem is here **/
feed.comments[index].name = 'Some random field';
console.log('Added new field' + util.inspect(feed));

});
}

}
}
});
Also the response is this without the missing .name field.

Added new field{ _id: 4f34f343c7b0434217000012,
original_link: 'http://com',
publish_date: Fri, 10 Feb 2012 10:36:00 GMT,
summary: 'some text',
title: 'title exampel',
comments:
[ { body: 'well',
author: 4f30265e6f60dc061d000002,
_id: 4f34f3b6f96c58541700000f,
create_date: Fri, 10 Feb 2012 10:38:46 GMT } ],
create_date: Fri, 10 Feb 2012 10:36:51 GMT }
// EDIT more information

Well i haven't found the answer but some how the
console.log(feed.comments[index]) returns reference to function. Maybe
someone who has more experience with mongoosejs could explain what
would be workaround in this situation.

{ [Function] author: 'Some random field' }


The schemas are in separate files models/User models/Rss

Aaron Heckmann

unread,
Feb 10, 2012, 10:57:21 AM2/10/12
to mongoo...@googlegroups.com
few things.

first:

iterating an array using `for in` can lead to problems when a crazy person has carelessly modified either Array.prototype or Object.prototype. don't do it. instead use a for loop or while loop or arr.forEach() or whatever. just not `for in`. notice I said "careless person" b/c there are ways of extending these prototypes that avoid this issue.

next:


   feed.comments[index].name = 'Some random field';

index here will always be the last index b/c a ref to it is captured in each callbacks closure, not its value. to see a quick example of this:

$ node
> for (var i = 0; i < 10; ++i) { process.nextTick(function(){ console.log(i) }) }
> 10
> 10
> 10
...

the result is that the last item of the `comments` will be the only one with a name set. to fix this you could use `feed.comments.forEach(..)` which would also solve your first problem above.

last:

tacking on arbitrary properties to a mongoose document will not show up in console.log (even though they exist). to see these values, first convert the doc to its plain js representation using `.toObject()`, then tack on the properties.

  doc = doc.toObject();
  doc.whatever = 'asdf';
  console.log(doc);  // { whatever: 'asdf', _id: 'sd7f8ad7f8a9f7asd89fa7s8d9' }



--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en



--
Aaron


jurka

unread,
Feb 11, 2012, 9:28:42 AM2/11/12
to Mongoose Node.JS ORM
Thank you for your replay, I got my example working yes .toObjec()
created obj literal which can be modified but i cannot make return the
object as it's asnyc.
The other approach what i thought is to use DBrefs, but I am missing
some details here.
I have schema like this

// This is in models/rss.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;

var comment_schema = new Schema({
body: String
, author: { type: Schema.ObjectId, ref: 'User' }
, create_date: { type: Date, default: Date.now }
}, { strict: true });

var rss_schema = new Schema({
title: { type: String, index: { unique: true } }
, summary: String
, original_link: String
, publish_date: Date
, create_date: { type: Date, default: Date.now }
, comments : [comment_schema]
, rss_id: ObjectId
}, { strict: true });

module.exports = mongoose.model('Rss', rss_schema);

//rss/user.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;

var user_schema = new Schema({
accessToken: String
, name: String
, email: { type: String, index: true, unique: true}
, facebook_id: String
}, { strict: true });

module.exports = mongoose.model('User', user_schema);


//And code with populate is here
// This is in app.js
Rss.findById(itemId).populate('author')
.run(function(err, feed) {
if (err) {
console.log('Found error');
} else {
// The feed comments should contain the author data from
User ???
console.log(feed);
}
});
> >http://stackoverflow.com/questions/9226694/modifying-mongoose-object-...
Reply all
Reply to author
Forward
0 new messages