florianholzapfel / express-restify-mongoose

Easily restify mongoose databases
https://florianholzapfel.github.io/express-restify-mongoose/
MIT License
640 stars 155 forks source link

virtuals are not working with GET route. #371

Closed abhishekvaid closed 3 years ago

abhishekvaid commented 6 years ago

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}) });`

Zertz commented 6 years ago

You can try turning off the lean option,

expressRestifyMongoose.serve(app, userModel, {
  lean: false,
  totalCountHeader: true
});
OliverFischer commented 3 years ago

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?

Zertz commented 3 years ago

This might be a typo but make sure you're setting lean: false

OliverFischer commented 3 years ago

@Zertz : Please apologize, of course my code is:

options: {
        strict: false,
        lean: false,
        toObject: {
            virtuals: true
        },
        toJSON: {
            virtuals: true
        }
    },
OliverFischer commented 3 years ago

@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.

Zertz commented 3 years ago

No worries. Glad you got it fixed!