代码之家  ›  专栏  ›  技术社区  ›  samynmhd

无法设置未定义的属性“name”

  •  0
  • samynmhd  · 技术社区  · 7 年前
    const mongoose = require('mongoose');
    
    let All = mongoose.Schema({
      school:{
          name:{
            type: String,
          },
          program:[{
            name: String,
            semester: Number,
            subject: [{name: String, crh: Number}]
          }]
      }
    })
    
    let allSub = module.exports = mongoose.model('allSub', All);
    

    我创建的猫鼬系列

    if(!course){
          let x = new All();
          x.school.name = School;
          x.school.program.name = Course;
          x.school.program.semester = Semester;
          x.school.program.subject.name = "One";
          x.save((err) =>{
            if(err) throw err;
            console.log("Date saved");
            res.render('addinsub');
          })
        }
    

    数据插入块。说x学校是错误的。程序主题未定义的名称。 如何克服这个错误?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Rahul Sharma    7 年前

    您需要先创建对象。

    const x = new All();
    x.school = {
        name: Course,
        program: [{
            semester: Semester,
            subject: {
                name: "one"
            }
        }]
    };
    x.save((err) => {
        if (err) throw err;
        console.log("Date saved");
        res.render('addinsub');
    })
    
        2
  •  0
  •   emtei    7 年前

    你需要x学校。程序主题是对象。

    if(!course){
      let x = new All();
      x.school.name = School;
      x.school.program.name = Course;
      x.school.program.semester = Semester;
      x.school.program.subject = {name: "One"};
      x.save((err) =>{
        if(err) throw err;
        console.log("Date saved");
        res.render('addinsub');
      })
    }