Skip to content

Instantly share code, notes, and snippets.

@thatmarvin
Created April 19, 2013 22:17
Show Gist options
  • Save thatmarvin/5423601 to your computer and use it in GitHub Desktop.
Save thatmarvin/5423601 to your computer and use it in GitHub Desktop.
Setter isn't invoked when value === null
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var assert = require('assert');
mongoose.connect('mongodb://localhost/test');
var OrderSchema = new Schema({
total: {
type: Number,
default: 0,
set: function (value) {
return (value || 0) + 10;
}
}
});
var Order = mongoose.model('offer', OrderSchema);
var TEST_VALUE = null; // Works fine when value !== null
var order = new Order({
total: TEST_VALUE
});
order.save(function (err, order) {
if (err) {
return console.error(err.message);
process.exit(1);
}
assert.equal(order.total, TEST_VALUE + 10, 'Setter is run');
process.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment