为什么这不起作用???。
var mongoose = require('mongoose');
var schema = new mongoose.Schema({
name: { type: String, required: true },
courses: [{ type: String, ref: 'Course' }]
});
/* Returns the student's first name, which we will define
* to be everything up to the first space in the student's name.
* For instance, "William Bruce Bailey" -> "William" */
schema.virtual('firstName').get(function(name) {
var split = name.split(' ');
return split[0];
});
错误:TypeError:无法读取属性' split'未定义的
答案 0 :(得分:0)
尝试在名称前添加“this”,以便您的代码看起来像这样
schema.virtual('firstName').get(function(name) {
var split = this.name.split(' ');
return split[0];
});