Created
September 30, 2012 04:25
-
-
Save mwheeler/3805833 to your computer and use it in GitHub Desktop.
Mongoose weirdness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
mongoose.connect("localhost", "subdoc_test"); | |
var SubDocSchema = new mongoose.Schema({ foo: String, bar: String }); | |
var mapFrom = function(x) { x.foo = x.abc; x.bar = x.xyz; delete x.abc; delete x.xyz; return x; } | |
var TestSchema = new mongoose.Schema({ | |
values: | |
{ | |
type: [SubDocSchema], | |
set: function(docs) { | |
console.log('set: ' + JSON.stringify(docs.map(mapFrom))); | |
docs.map(mapFrom); | |
return docs; | |
} | |
} | |
}); | |
var TestModel = mongoose.model("TestSchema", TestSchema); | |
TestModel.collection.drop(function(e){ console.log('drop error: ' + e);}); | |
// Setter seemingly called, but no values get saved..? | |
var test = new TestModel({ values: [ { abc: "foo", xyz: "bar" } ] }); | |
test.save(function(error) | |
{ | |
console.log('save error: ' + error); | |
TestModel.findOne({}, function(error, doc) | |
{ | |
console.log('findOne error: ' + error); | |
// Expect [ { foo: "foo", bar: "bar" } ] | |
console.log(doc.values); | |
process.exit(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment