Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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
restfulapi Node.js&;猫鼬:处理错误_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

restfulapi Node.js&;猫鼬:处理错误

restfulapi Node.js&;猫鼬:处理错误,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我想用Node.js、Express、MongoDB和Mongoose创建一个restful API 模式、模型和路线如下所示: // Schema var OperatorSchema = new Schema({ name : { type: String, unique: true , required: true}, short_name : { type: String, unique:true, required: true }, }); Operator = mon

我想用Node.js、Express、MongoDB和Mongoose创建一个restful API

模式、模型和路线如下所示:

// Schema
var OperatorSchema = new Schema({
    name : { type: String, unique: true , required: true},
    short_name : { type: String, unique:true, required: true },
});
Operator = mongoose.model('Operator', OperatorSchema);

// My Api for Operator
app.post('/api/operators', common.createOperator);
exports.createOperator = function(req, res){
    Operator.create(req.body, function (err, operator) {
        // If failed, return error message
        if (err) {
            console.log(err);
            res.send(404, err)
        }
        else {
            res.send(operator)
        }
    })
};
我有三个问题:

1-像这样处理错误我发现传递给回调的err对象具有不同的结构,这取决于错误是来自mongoose还是mongo

// Mongo Error:
{
    "name": "MongoError",
    "err": "E11000 duplicate key error index: ussdauto.operators.$name_1  dup key: { : \"OpTest\" }",
    "code": 11000,
    "n": 0,
    "connectionId": 231,
    "ok": 1
}

// Mongoose Error:
{
    "message": "Validation failed",
    "name": "ValidationError",
    "errors": {
    "short_name": {
        "message": "Path `short_name` is required.",
        "name": "ValidatorError",
        "path": "short_name",
        "type": "required"
      }
    }
}
我不想公开关于错误的所有细节:只需要一条解释错误的消息就足够了,不需要知道它是mongoerror还是validationError。你如何管理它

2-第二个问题更具全局性:我将有多个API,每次我都需要做相同的工作:检查是否有错误,是否有一些通过JSON中的API返回错误消息,如果没有正常继续。如何仅使用一个函数来处理错误并在任何地方使用它


3-我来自一个python世界(事实上是Django),在那里,Tastypie是一个非常棒的模块,它自己处理所有这些东西。您知道Node.js+Express是否存在这样的模块吗?

您可以使用一个名为processMongooseError(err)的函数创建一个Utils模块(或者您喜欢的函数)。然后,您可以在函数中执行一个简单的检查,以确定错误的类型,然后返回一条简单的错误消息。下面我举了一个例子来说明我所说的

module.exports.processMongooseError = function(err) {
    if(err.name == "MongoError") {
        // mongo db error

    } else if(err.name == "ValidationError") {
        // mongoose error

    }
}
这个模块可以在你需要的任何地方使用,应该可以解决你的第二个问题

"short_name": {
        "message": "Path `short_name` is required.",
此错误是因为,在架构中,此字段是必需的

var OperatorSchema = new Schema({
    name : { type: String, unique: true , required: true},
    short_name : { type: String, unique:true, required: true },
当您未通过表单或邮递员应用程序提供任何输入时。。不能将必填字段留空

我不知道是什么模块导致了错误 但你可以通过观察错误来猜测

"name": "MongoError",
    "err": "E11000 duplicate key error

此错误是因为您将名称定义为唯一,但尝试再次插入相同的名称。

如果这仍然是一个问题,我将面临相同的问题,并使用此mongoose插件使错误消息更加一致。


该插件将接受mongodb错误并将其转换为类似mongoose的错误

如果您不想在向MongoDB发出POST请求后显示完整的错误响应,您可以使用Promissions作为模型来链接响应,并向RESTFull API的用户显示自定义内容,以获得json响应

我将为您提供一个示例,作为构建API的良好指南:

1#模式模型/api/模型/运算符

const mongoose = require('mongoose');

const OperatorSchema = new Schema({
    name : { type: String, unique: true , required: true},
    short_name : { type: String, unique:true, required: true },
});

module.exports = mongoose.model('Operator', OperatorSchema);
// let's imagine that at this point you have already set up your form somewhere in your app and you are using body-parser, you have some Headers configuration for this route.


//import the libraries to execute your CRUD

const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');//generates json structure from incoming form post
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

const Operators = require('../api/models/operators');

router.post('/api/operators', (req, res, next) => {

    const operator = new Operator({
        _id: new mongoose.Types.ObjectId(),
        name: req.body.name, //get the form field of "operator" using body-parser
        short_name: req.body.short_name // other fiels... from the Schema model
    }); // Build an object to store to the database using the schema set up

    // The following chain method returns a promise which you can build your custom responses for this post,
    // This chain method can be used for POST GET and DELETE requests 

    operator.save().then(result => {
        res.status(201).json({
            message: "Operator Created", //
            result: result // the "result" object can be filtered or you can simply return the message
        });

        // THE RESULT OBJECT PROPERTIES YOU CAN FILTER ON RETURN
        //  "result" : {
        //      name: "some operator name",
        //      short_name: "short guy",
        //      __v : 0,
        //      _id : "mongodb-id-generated"
        // }

    }).catch(err => {
        console.log(err);
        res.status(500).json({
            error: err 
        });
    });

}); // #### POSTS NEW OPERATORS
2#操作员积垢文件(后期)处理。/api/routes/Operator

const mongoose = require('mongoose');

const OperatorSchema = new Schema({
    name : { type: String, unique: true , required: true},
    short_name : { type: String, unique:true, required: true },
});

module.exports = mongoose.model('Operator', OperatorSchema);
// let's imagine that at this point you have already set up your form somewhere in your app and you are using body-parser, you have some Headers configuration for this route.


//import the libraries to execute your CRUD

const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');//generates json structure from incoming form post
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

const Operators = require('../api/models/operators');

router.post('/api/operators', (req, res, next) => {

    const operator = new Operator({
        _id: new mongoose.Types.ObjectId(),
        name: req.body.name, //get the form field of "operator" using body-parser
        short_name: req.body.short_name // other fiels... from the Schema model
    }); // Build an object to store to the database using the schema set up

    // The following chain method returns a promise which you can build your custom responses for this post,
    // This chain method can be used for POST GET and DELETE requests 

    operator.save().then(result => {
        res.status(201).json({
            message: "Operator Created", //
            result: result // the "result" object can be filtered or you can simply return the message
        });

        // THE RESULT OBJECT PROPERTIES YOU CAN FILTER ON RETURN
        //  "result" : {
        //      name: "some operator name",
        //      short_name: "short guy",
        //      __v : 0,
        //      _id : "mongodb-id-generated"
        // }

    }).catch(err => {
        console.log(err);
        res.status(500).json({
            error: err 
        });
    });

}); // #### POSTS NEW OPERATORS

我在相当长的一段时间内使用了这个过程,允许开发人员将内容发布到API是非常有效的,如果他们出现错误,我会向他们展示他们需要什么来更好地阐述他们的请求

如果你每篇文章只问一个问题,效果会更好。