1
我試圖讓從全名獨立firstName和lastName學習的目的。當我去運行這個應用程序,我得到兩個錯誤)貓鼬架構學生有一個「的firstName」虛擬B)的貓鼬架構學生有一個「lastName的」虛擬從全名獲得firstName和lastName MongoDB中堅持的Node.js
下面是代碼我調試
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').set(function(name) {
var split = name.split(' ');
this.firstName = split[0];
});
/* Returns the student's last name, which we will define
* to be everything after the last space in the student's name.
* For instance, "William Bruce Bailey" -> "Bailey" */
schema.virtual('lastName').set(function(name) {
var split = name.split(' ');
this.lastName = split[split.length - 1];
});
module.exports = schema;
感謝 – DotNET