These are chat archives for LearnBoost/mongoose

16th
Jan 2015
Ilan Biala
@ilanbiala
Jan 16 2015 01:28
I'm working with passport and Mongoose, and I am trying to modify a subdocument in a populated array of req.user before I send it. How can I modify the subdocument and make sure the change sticks when I send it over HTTP?
Maksim
@chetverikov
Jan 16 2015 01:57
@ilanbiala Wath you want to do with data? Maybe you will help transform option of toObject? http://mongoosejs.com/docs/api.html#document_Document-toObject
example from my code:
Ilan Biala
@ilanbiala
Jan 16 2015 02:00
The issue I found is actually in my virtual setter.
Maksim
@chetverikov
Jan 16 2015 02:00
ok
Ilan Biala
@ilanbiala
Jan 16 2015 02:01
OrderSchema.virtual('toUpdate').set(function(toUpdate) {
    this.toUpdate = toUpdate;
});
This code throws a maximum call stack error.
Maksim
@chetverikov
Jan 16 2015 02:01
@ilanbiala is bad code +)
Ilan Biala
@ilanbiala
Jan 16 2015 02:01
@chetverikov any ideas on what's going on?
What should it be?
Maksim
@chetverikov
Jan 16 2015 02:03
You did a virtual set and call it inside himself +) rename toUpdate, example this._toUpdate
Ilan Biala
@ilanbiala
Jan 16 2015 02:03
Rename the virtual property?
So the code should be that?
OrderSchema.virtual('_toUpdate').set(function(toUpdate) {
    this._toUpdate = toUpdate;
});
Maksim
@chetverikov
Jan 16 2015 02:07

OrderSchema.virtual('toUpdate').set(function( value ) {
    this._toUpdate = value;
});

// and...

myDocument.toUpdate = 123123; // myDocument._toUpdate === 123123

myDocument.toObject(); // no toUpdate or _toUpdate
myDocument.toObject( {virtuals: true} ); // exist toUpdate, but no _toUpdate
Ilan Biala
@ilanbiala
Jan 16 2015 02:07
So should the getter return this._toUpdate?
Maksim
@chetverikov
Jan 16 2015 02:09
OrderSchema.virtual('toUpdate').get(function() {
    return this._toUpdate;
});

// or chain

OrderSchema.virtual('toUpdate').set(function( value ) {
    this._toUpdate = value;
}).get(function() {
    return this._toUpdate;
});
and, this field will not be in mongodb if you do not set the option in schema
Ilan Biala
@ilanbiala
Jan 16 2015 02:13
Yes, I only want it to be sent to the client. And what's the difference between getters and virtuals with toJSON and toObject?
Maksim
@chetverikov
Jan 16 2015 02:14
Hmmm... Nothing +) toJSON call toObject =)
Ilan Biala
@ilanbiala
Jan 16 2015 02:15
Right, but what is the difference between getters: true and virtuals: true?
Maksim
@chetverikov
Jan 16 2015 02:15
Oh. How bad my English =) +) +)

http://mongoosejs.com/docs/api.html#document_Document-toObject

getters apply all getters (path and virtual getters)
virtuals apply virtual getters (can override getters option)

The getter is created on the existing path, and virtual path is a virtual +)

Ilan Biala
@ilanbiala
Jan 16 2015 02:20
So which is broader?
getters?
Maksim
@chetverikov
Jan 16 2015 02:21
Hm... Virtuals most likely, but it all depends on the task
Ilan Biala
@ilanbiala
Jan 16 2015 02:35
Is it wrong if I set both true?
Maksim
@chetverikov
Jan 16 2015 02:37
What are you set to true?
Ilan Biala
@ilanbiala
Jan 16 2015 03:02
both virtuals and getters.
Maksim
@chetverikov
Jan 16 2015 03:09
Is not it wrong +)
Ilan Biala
@ilanbiala
Jan 16 2015 03:11
So I'm not getting an error anymore, but when I log the document, toUpdate is undefined. When I send it over HTTP, toUpdate doesn't show up. What am I missing?
Maksim
@chetverikov
Jan 16 2015 03:11
show me your code
Ilan Biala
@ilanbiala
Jan 16 2015 03:12
The schema, or the part where I modify the document?
Maksim
@chetverikov
Jan 16 2015 03:12
And then and then
Ilan Biala
@ilanbiala
Jan 16 2015 03:14
Schema:
var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var OrderSchema = new Schema({
    customer: {
        type: String,
        required: true
    },
    items: [{
        type: Schema.ObjectId,
        ref: 'Item',
        required: true
    }],
    quantity: [{
        type: Number,
        required: true
    }],
    total: {
        type: Number,
        required: true
    },
    created: {
        type: Date,
        default: Date.now
    },
    updated: {
        type: Date,
        default: Date.now
    }
}, {
    toJSON: {
        getters: true,
        virtuals: true
    },
    toObject: {
        getters: true,
        virtuals: true
    }
});

OrderSchema.virtual('toUpdate').get(function() {
    return this._toUpdate;
}).set(function(toUpdate) {
    return this._toUpdate = toUpdate;
});

OrderSchema.pre('save', function(next) {
    this.updated = new Date();
    return next();
});

mongoose.model('Order', OrderSchema);
This code runs after getting a user which has an array orderHistory populated with orders:
if (user.orderHistory.length) {
    var lastOrder = user.orderHistory[user.orderHistory.length - 1];
    lastOrder.set('toUpdate', schedule.isBetween(lastOrder.created), Boolean);
}
Maksim
@chetverikov
Jan 16 2015 03:20

So…

1) You will only need to use option toObject with virtual: true
2) You can set the value in virtual field:

if (user.orderHistory.length) {
    var lastOrder = user.orderHistory[user.orderHistory.length - 1];
    lastOrder.toUpdate = Boolean(schedule.isBetween(lastOrder.created));
}
Ilan Biala
@ilanbiala
Jan 16 2015 03:23
I don't need to do .set('toUpdate')?
Maksim
@chetverikov
Jan 16 2015 03:26
You can do so and so.
Ilan Biala
@ilanbiala
Jan 16 2015 03:26
Okay. Thanks for the help.
Maksim
@chetverikov
Jan 16 2015 03:39
Oh, don't mention it +)
Andrew Newdigate
@suprememoocow
Jan 16 2015 12:49
http://plugins.mongoosejs.com/ is currently down. I guess you need to update the DNS in the same way as you did for http://mongoosejs.com?