I have Customer schema:
var CustomerSchema = new mongoose.Schema({
name: { type: String, required: true },
lastname: { type: String, required: true },
comment: { type: String }
}, {
toJSON: { virtuals: true },
toObject: { virtuals: true }
});
const Customer = mongoose.model('Customer', CustomerSchema);
I want to add fullname field that return name + lastname.
But the fullname should not return by default only when specify the field, something like populate function does.
for example:
const c = Customer.find().populate('fullname'); // c.fullname
How to do that with mongoose?
** Edit ** I try to do that with virtual:
CustomerSchema.virtual('fullname').get(() => {
return 'some';
});
But I get the error:
If you are populating a virtual, you must set the localField and foreignField