所以,我正在尝试使用JSON格式的一些示例数据为数据库设定种子。这是功能:
request(options, (err, res, body) => {
if (!err && body) {
var classId;
body.forEach((element, i) => {
Student.create(element, (err, success) => {
if (err) {
console.log("This is a error", err)
} else if (i % 4 === 0) {
console.log(`This is the division for ${i}`, i % 4)
let testClass = new Class();
console.log(testClass)
testClass.name = `Class ${i}`;
testClass.students.push(success._id);
testClass.save((err, successClass) => {
console.log("This is the class created", successClass)
classId = successClass._id;
console.log("This is a the class id", classId)
User.findOne({ email: 'admin@example.com' }, (err, foundUser) => {
foundUser.classId.push(successClass._id);
})
})
} else {
console.log(classId)
Class.findById(classId, (err, foundClass) => {
foundClass.students.push(success._id);
foundClass.save();
})
}
})
})
}
});
我遇到了这个代码的两个不同问题,1。Mongoose创建了一些不完整的文档。 2. ClassId变量无法存储该值。
对于第二个问题,我会问一个不同的问题。
这里的逻辑是我有20名学生的样本,我想创建5个小组,每个小组有4名学生。
以下是我在控制台中的内容
This is the division for 0 0
{ _id: 595b028dc7000c18b71d14b5,
activeStaus: true,
students: [],
dateCreated: 2017-07-04T02:50:47.296Z,
teachers: [] }
undefined
undefined
This is the division for 4 0
{ _id: 595b028dc7000c18b71d14b6,
activeStaus: true,
students: [],
dateCreated: 2017-07-04T02:50:47.296Z,
teachers: [] }
This is the division for 8 0
{ _id: 595b028dc7000c18b71d14b7,
activeStaus: true,
students: [],
dateCreated: 2017-07-04T02:50:47.296Z,
teachers: [] }
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
This is the division for 16 0
{ _id: 595b028dc7000c18b71d14b8,
activeStaus: true,
students: [],
dateCreated: 2017-07-04T02:50:47.296Z,
teachers: [] }
This is the division for 12 0
{ _id: 595b028dc7000c18b71d14b9,
activeStaus: true,
students: [],
dateCreated: 2017-07-04T02:50:47.296Z,
teachers: [] }
undefined
undefined
undefined
This is the class created { __v: 0,
name: 'Class 4',
_id: 595b028dc7000c18b71d14b6,
activeStaus: true,
students: [ 595b028dc7000c18b71d14a5 ],
dateCreated: 2017-07-04T02:50:47.296Z,
teachers: [] }
This is a the class id 595b028dc7000c18b71d14b6
This is the class created { __v: 0,
name: 'Class 8',
_id: 595b028dc7000c18b71d14b7,
activeStaus: true,
students: [ 595b028dc7000c18b71d14a9 ],
dateCreated: 2017-07-04T02:50:47.296Z,
teachers: [] }
This is a the class id 595b028dc7000c18b71d14b7
This is the class created { __v: 0,
name: 'Class 0',
_id: 595b028dc7000c18b71d14b5,
activeStaus: true,
students: [ 595b028dc7000c18b71d14a1 ],
dateCreated: 2017-07-04T02:50:47.296Z,
teachers: [] }
This is a the class id 595b028dc7000c18b71d14b5
正如您在日志中看到的那样,前几个文档没有name属性,并且他们有空学生数组。
我认为它有一些事情要做迭代,因为在我的脑海中,i
的值将按顺序为0,1,2,3,4 ... 19,即使不是这种情况,我仍然无法理解不完整的文件。
编辑:这是classSchema和studentSchema
学生架构
var StudentSchema = new Schema({
name: String,
role: String,
added: { type: Boolean, default: false },
email: {
type: String,
lowercase: true
},
password: String,
provider: String,
pin: String,
school: String,
salt: String,
schoolId: mongoose.Schema.Types.ObjectId,
classId: mongoose.Schema.Types.ObjectId,
teacherId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
className: String,
activeStaus:{type:Boolean,default:true},
grade: String,
grade_id: Number,
teacheName: String,
teacherNumber: String,
teacherEmail: String,
courseName: String,
courseNumber: String,
courseId: Number,
period: Number,
gender: String,
number: Number,
realisticInterests: Number,
investigativeInterests: Number,
artisticInterests: Number,
socialInterests: Number,
entrepreneurialInterests: Number,
conventionalInterests: Number,
careerDecidedness: Number,
neuroticism: Number,
extraversion: Number,
opennessToExperience: Number,
conscientiousness: Number,
agreeableness: Number,
activeLearner: Number,
reflectiveLearner: Number,
theoreticalLearner: Number,
progmaticLearner: Number,
socialDesirability: Number
});
类架构
var classSchema = new Schema({
schoolId: mongoose.Schema.Types.ObjectId,
teachers:Array,
dateCreated:{type:Date,default:Date.now()},
name : String,
students:[{type:mongoose.Schema.Types.ObjectId,ref:'User'}],
activeStaus:{type:Boolean,default:true}
});
示例数据
我无法发布完整的数据,因为它会过多地填充此线程,并且我已经彻底检查了数据,它不是任何必需的属性。这是一个样本
{
"Sr. No.": 1,
"school_name": "Franklin High School",
"school_number": 1001,
"school_id": "",
"grade": 11,
"grade_id": "",
"teacher_name": "Merideth Merrill",
"teacher_number": 539224,
"teacher_id": "",
"teacher_email": "",
"course_name": "Physics 1",
"course_number": 101,
"course_id": "",
"period": 1,
"student_name": "Agnes Walters",
"student_gender": "",
"student_number": "",
"student_id": 1000,
"student_pin": "",
"student_email": "",
"realistic_interests": 1.55,
"investigative_interests": 4.55,
"artistic_interests": 4.92,
"social_interests": 4.96,
"entrepreneurial_interests": 1.98,
"conventional_interests": 4.5,
"career_decidedness": 4.66,
"neuroticism": 2.49,
"extraversion": 1.98,
"openness_to_experience": 1.92,
"conscientiousness": 2.14,
"agreeableness": 0.71,
"active_learner": 4.86,
"reflective_learner": 4.08,
"theoretical_learner": 4.99,
"progmatic_learner": 3.18,
"social_desirability": 0.92
},