我有一个名为应用程序的模型:
var ApplicationSchema = new mongoose.Schema({
name : {type: String, validate: [uniqueName, 'Unique Name']},
dateCreated: Date,
containers : [ContainerSchema]
});
mongoose.model('Application', ApplicationSchema);
var Application = database.model('Application');
它在保存时调用一个名为 uniqueName 的验证函数:
function uniqueName(name)
{
console.log('In Unique Name function');
Application.find({}, function(error, documents) {
for(var i = 0; i < documents.length; i++) {
if(documents[i].name == name) {
console.log('About to return false');
return false;
}
}
});
return true;
}
稍后在代码中,我将一些数据放入模型中并保存:
newApplication.name = request.body.name;
newApplication.save(function(error) {
console.log('Callback for save');
if(error) {
console.log('error if statement');
response.statusCode = 409;
response.end();
}
console.log('Done with callback');
});
response.statusCode = 201;
response.end();
当我用一个不唯一的名称测试它时,我得到一个 201 响应和来自我的终端的以下输出:
In Unique Name function
Callback for save
Done with callback
About to return false
我做错了什么,或者这真的是猫鼬的比赛条件吗?