Node.js Mocha MongoDB Mongoose ObjectId ref在第一次';它';陈述
我正在为mongoose模型编写一些测试,它包含一个引用另一个模型的字段。我运行before()并创建一个对象,然后运行测试。在创建的回调中,我看到对象在正确的字段中有一个ID。在第一个“it”语句中,我还看到了ID和我的测试通过。在后面的每一个“it”语句中,它都会消失并为空。我发现,如果我确实事先创建了被引用对象,那么该字段仍然存在。我不认为mongoose/mongo实际上检查了所有ObjectId引用,但如果检查了,有人知道它是如何工作的吗?如果不是,到底是什么导致了这种现象 消失的字段是OfficeHoursSchema中的“主机”字段。顺便说一句,即使required设置为false,这仍然不起作用 模型定义:Node.js Mocha MongoDB Mongoose ObjectId ref在第一次';它';陈述,node.js,mongodb,mongoose,mocha.js,Node.js,Mongodb,Mongoose,Mocha.js,我正在为mongoose模型编写一些测试,它包含一个引用另一个模型的字段。我运行before()并创建一个对象,然后运行测试。在创建的回调中,我看到对象在正确的字段中有一个ID。在第一个“it”语句中,我还看到了ID和我的测试通过。在后面的每一个“it”语句中,它都会消失并为空。我发现,如果我确实事先创建了被引用对象,那么该字段仍然存在。我不认为mongoose/mongo实际上检查了所有ObjectId引用,但如果检查了,有人知道它是如何工作的吗?如果不是,到底是什么导致了这种现象 消失的字段
var appointmentSchema = new Schema({
attendee: { type: Schema.Types.ObjectId, ref: "Student", required: false },
startTime: { type: Date },
duration: Number
});
var statusStates = ['open','closed']
var OfficeHoursSchema = new Schema({
host: { type: Schema.Types.ObjectId, ref: "Employee" , required: true},
appointments: [appointmentSchema],
description: String,
location: String,
startDateTime: { type: Date, default: Date.now },
endDateTime: { type: Date, default: Date.now },
status: { type: String, default: 'open' , enum: statusStates},
seqBooking: {type: Boolean, default: true}
});
测试:
describe('OfficeHours Model', function(){
var hostId = new mongoose.Types.ObjectId;
var mins = 15;
var numAppointments = ohDurationInMs/appointmentDuration;
var officeHour;
var officeHourToCreate = {
host: hostId,
appointments: appointmentsToCreate(),
description: 'meeting',
location: 'room 1',
startDateTime: new Date(startTime), //3/6/2015 at 3:30pm EST
endDateTime: new Date(endTime), //2 hours later. 3/6/2015 at 5:30pm EST
totalDuration: ohDurationInMs/(60*1000)
};
before(function(done){
OfficeHour.create(officeHourToCreate,function(err,createdOH){
officeHour = createdOH;;
done();
});
});
it('1st It statement',function(){
expect(officeHour.host).to.be.ok;
});
it('2nd It statement',function(){
expect(officeHour.host).to.be.ok;
});
});
第一条it语句通过,但第二条.host字段为空
基本上是这样的
工作:
before(function(done){
var employee = new Employee({password: 'asdfasdfsafasdf'});
employee.save(function(err,createdEmployee){
officeHourToCreate.host = createdEmployee._id;
OfficeHour.create(officeHourToCreate,function(err,createdOH){
officeHour = createdOH;;
done();
});
})
});
我觉得ObjectId是否存在于其他地方一定是在进行某种检查,但是有人能给我指出一些关于这种行为的文档吗?非常感谢你阅读这篇文章 我认为这是因为第二次调用
before()
时,您引用了实例化的ObjectId,而第二次调用时,它并没有像您所期望的那样创建一个新的对象
你能试试这个吗
before(function(done){
OfficeHour.create({
host: new mongoose.Types.ObjectId,
// btw I've never seen that syntax for making a new random ID, but if it does what you want it to do then go ahead!
appointments: appointmentsToCreate(),
description: 'meeting',
location: 'room 1',
startDateTime: new Date(startTime), //3/6/2015 at 3:30pm EST
endDateTime: new Date(endTime), //2 hours later. 3/6/2015 at 5:30pm EST
totalDuration: ohDurationInMs/(60*1000)
}, function(err,createdOH){
officeHour = createdOH;;
done();
});
});
您希望它做什么var hostId=new mongoose.Types.ObjectId代码>?我希望它会给我一个随机生成的ID,类型是mongoose中特定的ObjectId类型。我所拥有的和您正在尝试的之间到底有什么区别?我已经用你的代码替换了我的before函数,第二条语句仍然失败。另外,afaikbefore意味着只调用一次,所有的“it”语句都在后面运行。顺便说一句,我注意到这在beforeach而不是beforeach中起作用,特别是因为我只能获取主机字段一次,当它每次创建一个新对象时,我可以在每个“it”块中访问它一次。奇怪的