mongoose从find方法获取数据并将其插入其他模式

时间:2017-04-27 01:49:44

标签: node.js mongoose mongoose-schema

我正在尝试构建一个Web应用程序。

在网站上,我需要从mongodb获取带有mongoose的教师数据,其中一些将被插入其他模式(课程)。

if (!req.body) return console.log("No data sent");
    var newcourse;
    var iname = req.body.iname;
    var results;


/*ilist.find({ name: iname }).toArray((err, result) => {
    if (err) return console.log(err);
    results = result;
});*/
dbs.collection('instructors').find({ name: iname }).toArray((err, result) => {
    results = result;
    //console.log(result);        
});
// console.log(ilist.data);

newcourse = new clist({
    'coursename': req.body.coursename, 'coursenumber': req.body.coursenumber, 'coursecredit': req.body.coursecredit
    , 'courseroom': req.body.room, 'instructors.name': req.body.iname, 'instructors.email': results.email, 'instructors.phone': results.phone, 'instructors.role': req.body.role
});

newcourse.save(function (err) {
    if (err) {
        console.log(err);
        res.status(400);
        res.send(err);
    }
    else {
        res.status(200);
        console.log('A new course has been registered!');
        res.redirect('/course');
    }
});

-----架构-------------------

var instructorlist = mongoose.Schema({
    name: { type: String, required: true },
    age: { type: Number, required: true },
    gender: { type: String, required: true },
    DOB: { type: Date, required: true, default: Date.now },
    email: { type: String, required: true },
    phone: { type: Number, required: true },
    address: { type: String, required: true },
    dateofstart: { type: Date, required: true},
    courses: {
        coursename: { type: String, required: false },
        coursenumber: { type: Number, requird: false },
        coursecredit: { type: Number, required: false },
        courseroom: { type: String, required: false }
    }
});
var courselist = mongoose.Schema({
    coursename: { type: String, required: true },
    coursenumber: { type: String, required: true },
    coursecredit: { type: Number, required: true },
    courseroom: { type: String, required: false },
    courseregisteddate: {type: Date, default: Date.now},
    students: {
        name: { type: String, required: false },
        phone: { type: Number, requird: false },
        email: { type: String, required: false },
        class: { type: String, required: false }
    },
    instructors: {
        name: { type: String, required: false },
        phone: { type: Number, requird: false },
        email: { type: String, required: false },
        role: { type: String, required: false }
    }
});

我想获得instuctorlist的电子邮件和电话。(其他人来自req.body)和 把它放入新课程变量中以保存mongodb。 感谢您阅读。

1 个答案:

答案 0 :(得分:0)

教师查找函数将被异步调用,因此javascript代码将在您获得该mongoose查询的结果之前继续。

您需要在回调中添加课程的保存。可能看起来像这样:

if (!req.body) return console.log("No data sent");

dbs.collection('instructors').find({
  name: req.body.iname
}).toArray((err, instructors) => {
  if (!err) {
    if (!instructors) {
      var newcourse = new clist({
        // new data also from instructors
      });

      newcourse.save(function(err) {
        if (err) {
          res.status(400);
          res.send(err);
        }
        else {
          res.status(200);
          res.redirect('/course');
        }
      });
    }
    else {
      res.status(400);
      res.send("Could not find instructor");
    }
  }
  else {
    res.status(400);
    res.send(err);
  }
});

但要注意,通过这个,您将获得具有给定名称的所有教师,您必须从该数组中选择一个。如果要将更多课程保存到教师,则必须更改架构: e.g。

var courseSchema = mongoose.Schema({
  // Schema variables
  ...

});

var courseModel = mongoose.model("Course", courseSchema);

var instructorlist = mongoose.Schema({
  name: {
    type: String,
    required: true
  },

  ...

  courses: [{
    type: ObjectId,
    ref: "Course"
  }]
});