【发布时间】:2019-01-15 21:14:15
【问题描述】:
我尝试了几种不同的方法来验证 Mongoose 中的外键,但无法弄清楚。
我有这样的架构:
//Doctors.js
var schema = mongoose.Schema({
email: { type: String }
}
module.exports = mongoose.model('Doctors', schema);
//Patients.js
var Doctors = require('./Doctors');
var schema = mongoose.Schema({
email: { type: String },
doctor: { type: String, ref: 'Doctors' }
}
schema.pre('save', function (next, req) {
Doctors.findOne({email:req.body.email}, function (err, found) {
if (found) return next();
else return next(new Error({error:"not found"}));
});
});
module.exports = mongoose.model('Patients', schema);
但是我收到了这个错误:Uncaught TypeError: Object #<Object> has no method 'findOne'
有人知道如何做类似于我在这里尝试做的事情吗?
【问题讨论】:
-
所以,我可以使用 mongoose 4.0.5 和 mongo 2.4.6 让它在我的机器上运行。当您
console.log(Doctors)时,您会得到什么? -
我收到
{}。但我刚刚找到答案,给我一秒钟发布它