Closed abhishekvaid closed 3 years ago
You can try turning off the lean
option,
expressRestifyMongoose.serve(app, userModel, {
lean: false,
totalCountHeader: true
});
Hi, @Zertz : The option lean : true
does not take any effect in my model. Do you have some more informations why it is not working in combination with express-restify-mongoose?
This might be a typo but make sure you're setting lean: false
@Zertz : Please apologize, of course my code is:
options: {
strict: false,
lean: false,
toObject: {
virtuals: true
},
toJSON: {
virtuals: true
}
},
@Zertz : Sorry again, I think i found it, the error is on our side due to our poor implementation of wrapping json-schema definitions to mongoose schemas and forgetting to pass the options correctly to erm.
No worries. Glad you got it fixed!
I have a simple model with a virtual field. I've also declared
clusterSchema.virtual('url').get( function () { return this.vertical ; });
AND{ timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }, toObject: { virtuals: true }, toJSON: { virtuals: true }, }
on the schema. However, when I do a get, I don't see virtual field set. When I manually call toObject or toJSON, I see the field.Another Simple App in which GET doesn't return virtual
`let expressRestifyMongoose = require('express-restify-mongoose'), mongoose = require('mongoose'), express = require('express'), bodyParser = require('body-parser'), methodOverride = require('method-override') ;
mongoose.Promise = global.Promise; mongoose.connect('mongodb://xxxxxxxxxxxxxxx@localhost:27017/test_db?authSource=admin', {useMongoClient: true});
let userSchema = mongoose.Schema( { first: String, last: String }, { timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }, toObject: { virtuals: true }, toJSON: { virtuals: true }, } );
userSchema.virtual('fullname').get(function() { return this.first + ' ' + this.last; });
let userModel = mongoose.model('user', userSchema);
let app = express(); app.use(bodyParser.json()); app.use(methodOverride());
expressRestifyMongoose.serve(app, userModel, {totalCountHeader: true});
app.listen(3000, () => { console.log(
Express server (main app) listening on port ${3000}
) });`