Skip to content

Instantly share code, notes, and snippets.

@laurie71
Created August 1, 2011 16:36
Show Gist options
  • Save laurie71/1118468 to your computer and use it in GitHub Desktop.
Save laurie71/1118468 to your computer and use it in GitHub Desktop.
mongoose setter mis-behaviour
var assert = require('assert')
, mongoose = require('mongoose')
, Schema = mongoose.Schema
, OID = Schema.ObjectId
, schema, model, conn, o
, log = console.error
schema = new Schema({ str: String })
schema.path('str').set(function(v) {
var nv = v+':'+v
console.log('SET <%s> -> <%s>', v, nv)
return nv
})
model = mongoose.model('model', schema)
assert.equal(model, mongoose.model('model'))
conn = mongoose.connect('mongodb://localhost/test')
model.remove(function(err) { if (err) throw err })
function save(o) {
log('SAVE', o)
o.save(function(err, o) {
log('SAVED', o)
assert.equal(o.str, 'x:x')
load()
})
}
function load() {
log('FIND')
model.find({}, function(err, os) {
log('FOUND', os)
assert.equal(1, os.length)
assert.equal(os[0].str, 'x:x')
})
}
log('NEW')
o = new model({ str: 'x' })
save(o)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment