【发布时间】:2017-05-05 17:50:40
【问题描述】:
如果我在schema.pre('save') 中的验证失败,我如何发送自定义错误消息?例如,如果我有一个聊天功能,您可以在其中创建一个新对话,我想检查与给定参与者的对话是否已经存在,所以我可以这样做:
ConversationSchema.pre('save', function(next, done) {
var that = this;
this.constructor.findOne({participants: this.participants}).then(function(conversation) {
if (conversation) {
// Send error back with the conversation object
} else {
next();
}
});
});
【问题讨论】: