我已经在Mongoose Schema中创建了一个类似于这样的虚拟方法:
UserSchema.virtual('fullName').get(function() {
return this.firstName + ' ' + this.lastName;
}).set(function(replacedName) {
this.set(this.firstName, replacedName);
});
在服务器上执行find()方法:
User.find({}).exec(function(error, users) {
// I want to use virtual method for users array
users.set('fullName', 'Name will be replaced');
});
我能在不使用循环的情况下为数组使用虚拟方法吗? 我正在使用NodeJS和Mongoose。