LearnBoost/mongoose — Issue #665: Setters run too late on properties of type: ObjectId
Author: hunterloftis
Created: 2011-12-28 20:53:17+00:00
State: open

```javascript

var Institution = new server.mongoose.Schema({
photo: { set: extract_id, type: server.mongoose.Schema.ObjectId, ref: 'Photo' }
});

function extract_id(obj) {
console.log("SETTING TO:", obj);
return (obj && obj._id) ? obj._id : obj;
}

```

This will be called... but *after* mongoose uses the Object instead of its _id and throws an error:

```
Error: Invalid ObjectId
at Function.fromString (/Users/hunter/work/skool/node_modules/mongoose/lib/drivers/node-mongodb-native/objectid.js:27:11)
at ObjectId.cast (/Users/hunter/work/skool/node_modules/mongoose/lib/schema/objectid.js:64:16)
at ObjectId.applyGetters (/Users/hunter/work/skool/node_modules/mongoose/lib/schematype.js:249:14)
at model.get (/Users/hunter/work/skool/node_modules/mongoose/lib/document.js:468:18)
at model.set (/Users/hunter/work/skool/node_modules/mongoose/lib/document.js:329:19)
at model.set (/Users/hunter/work/skool/node_modules/mongoose/lib/document.js:293:16)
at model.set (/Users/hunter/work/skool/node_modules/mongoose/lib/document.js:287:16)
at model.Document (/Users/hunter/work/skool/node_modules/mongoose/lib/document.js:45:17)
at model.Model (/Users/hunter/work/skool/node_modules/mongoose/lib/model.js:26:12)
at new model (/Users/hunter/work/skool/node_modules/mongoose/lib/model.js:838:11)
```