我有一个Fee
模式,用于保存费用支付的详细信息,这些详细信息具有student
属性的引用,该属性是对Student
模式的引用,告诉谁支付了费用。
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Student = require('./student');
const fee = new Schema({
student: { // Student ID
type: Schema.Types.ObjectId,
ref: 'Student'
},
paidFor: {
type: Date
},
paidOn: {
type: Date,
default: Date.now
},
due: Number,
fine: Number,
late: Number,
misc: Number,
tution: Number,
library: Number,
admission: Number,
development: Number,
electricity: Number,
examination: Number
});
// This pre save middleware to check if the Student ID belongs to some student or not.
fee.pre('save', function(next) {
const { student } = this;
Student.findOne({ _id: student }, (error, result) => {
if (error) {
return next(new Error(error));
}
if (!result) { // If student not found.
return next(new Error("Student not found."));
}
return next();
});
});
module.exports = new mongoose.model("Fee", fee);
问题出在pre('save')...
中间件上。当我为它提供一个有效的student id
时,它属于Student
集合中的某个文档,它将按预期方式成功通过pre('save')...
中间件后将其成功保存。
但是当我给它一个student id
集合中不属于任何文档的Student
时,它并没有保存我想要的内容,但是它给空对象作为错误 。我上面通过时,错误对象没有像"Student not found."
这样的消息。
我在Mongoose Docs和this answer中尝试了解决方案,但没有任何效果。
请告诉我其背后的原因以及如何解决。谢谢。
答案 0 :(得分:0)
删除您要传递给struct ContentView: View {
private let x: Double = 200.0
private let y: Double = 200.0
private let radius: CGFloat = 100.0
var body: some View {
GeometryReader { geo in
Rectangle()
.foregroundColor(Color.black)
Path { path in
path.addArc(center: CGPoint(x: self.x, y: self.y), radius: self.radius, startAngle: .degrees(0), endAngle: .degrees(360), clockwise: true)
}
.fill(RadialGradient(
gradient: Gradient(colors: [.red, .blue]),
center: UnitPoint(
x: CGFloat(self.x / Double(geo.size.width)),
y: CGFloat(self.y / Double(geo.size.height))
),
startRadius: self.radius * 0.30,
endRadius: self.radius
))
}
}
}
的虚假ID
或者只是让猫鼬创建一个新的mongoose.Schema.ObjectId(*YOUR_STRING_ID*)