Skip to content

Instantly share code, notes, and snippets.

@mwheeler
Created September 30, 2012 04:25
Show Gist options
  • Save mwheeler/3805833 to your computer and use it in GitHub Desktop.
Save mwheeler/3805833 to your computer and use it in GitHub Desktop.
Mongoose weirdness
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