Closed NitnekB closed 3 years ago
try like this:
import {Prop, Schema, SchemaFactory} from '@nestjs/mongoose';
import {Document} from 'mongoose';
@Schema({autoIndex: true, toJSON: {virtuals: true}})
export class User extends Document {
@Prop ({required: true, min: 2, max: 64})
firstName: string;
@Prop({required: true, min: 2, max: 64})
lastName: string;
@Prop({required: true, lowercase: true})
email: string;
@Prop({required: true, select: false})
password: string;
@Prop({default: false})
hasBeenValidated: boolean;
}
export const UserSchema = SchemaFactory.createForClass(User);
UserSchema.virtual('fullName').get(function() {
return `${this.firstName} ${this.lastName}`;
});
for the addition of this part concerning the virtuals I had talked about it here with Kamil, I had created a pull. see discussion here https://github.com/nestjs/docs.nestjs.com/pull/1646
I still have the branch in the fork, if in the future you change your mind I can do the pull again, no problem for me :-)
if you have difficulty use the discord channel for support
You can define virtuals in the same way as described in the official mongoose docs.
Thanks a lot Tomy and Kamil, works perfectly fine! I guess it was to obvious for me haha
You can try to use Mongoose's loadClass
, when the second parameter is true
, you can only load virtual properties.
I'm submitting a...
Current behavior
Cannot set properly virtual attribute on schema definition
Expected behavior
Easy way to concat string attributes on new field using mongoose virtual feature
Ex: see Nest documentation with
fullName
onUser
schemaMinimal reproduction of the problem with instructions
What is the motivation / use case for changing the behavior?
Just need to understand how virtual mongoose feature works with
@nestjs/mongoose
(and also how to declare some methods will be nice ;) )No documentation about those features :(
Environment
none