Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation 如何向主干视图显示mongoose错误_Validation_Backbone.js_Express_Mongoose_Schema - Fatal编程技术网

Validation 如何向主干视图显示mongoose错误

Validation 如何向主干视图显示mongoose错误,validation,backbone.js,express,mongoose,schema,Validation,Backbone.js,Express,Mongoose,Schema,所以,我有一个主干视图,我试图保存一个用户 this.model.save(user_details, { // this is backbone model error: function (model, errors) { }, success: function (model, response) { } }); 主干模型urlRoo

所以,我有一个主干视图,我试图保存一个用户

 this.model.save(user_details, { // this is backbone model
                error: function (model, errors) {

                },
                success: function (model, response) {
                }
            });
主干模型urlRoot指向一个后端函数,其中

// here user is a Mongoose schema
user.save(function (err) {
    if (err) {
        res.send(err.errors);

    }
});
我正在Mongoose模式中运行一些验证

如果验证失败,如何在主干视图上显示这些“err.errors”。
我可以在终端上看到控制台是否记录了错误,但无法将其发送回视图。

在查看“错误”对象后找到了解决方案

注意:mongodb中的错误(如unique、dup密钥)不在此格式中追加。因此,我们需要将它们更改为json并将其封装在res.errors中

In case of error in unique keys user.save(function (err) {

    if (err) {
       if(err.code!='undefined' && err.code=='11000')
           err.errors = {'email':{'message':'This unique value is already in db'}};
       res.send(500, err.errors);
    } 
In case of error in unique keys user.save(function (err) {

    if (err) {
       if(err.code!='undefined' && err.code=='11000')
           err.errors = {'email':{'message':'This unique value is already in db'}};
       res.send(500, err.errors);
    }