【问题标题】:Nodejs Express crash when id was wrongid 错误时 Nodejs Express 崩溃
【发布时间】:2021-10-06 09:23:42
【问题描述】:

我正在使用 Nodejs、Express、Mongoose 开发 REST 服务。我还在学习阶段。

我有一个问题。

例如,当我尝试使用 ID 拉取内容时,如果 ID 正确且对应,则它可以正常工作。但如果 ID 不正确或缺失或超过 24 个字符,则问题是服务崩溃。如果收到 24 个字符的 ID,我可以返回 404 并且服务不会崩溃。我怎样才能避免这个崩溃问题。

正确的请求(返回结果为 JSON):http://localhost:8080/api/v1/posts?id=615ccb9f89a5e20454306090

崩溃请求:http://localhost:8080/api/v1/posts?id=123

错误:

events.js:366
    throw err; // Unhandled 'error' event
    ^

Error [ERR_UNHANDLED_ERROR]: Unhandled error. ({ status: 500, error: 'Post not found' })
    at Function.emit (events.js:364:17)
    at D:\CMS\node_modules\mongoose\lib\model.js:4872:15
    at processTicksAndRejections (internal/process/task_queues.js:77:11) {
  code: 'ERR_UNHANDLED_ERROR',
  context: { status: 500, error: 'Post not found' }
}
[nodemon] app crashed - waiting for file changes before starting...

控制器层:

...
function get(req, res) {
    postService
        .get(req)
        .then((result) => result && res.status(200).json(result))
        .catch((e) => errorMessages(e, res));
}

服务层:

...
async function get(req) {
    const findParams = {};
    if (req.query.id) {
        findParams['_id'] = req.query.id.trim().substring(0, 24);
    }
    const post = await Posts.find(findParams, (e, post) => {
        if (e) throw { status: 500, error: `Post not found` };
        return post;
    }).sort({ createdDate: -1 });
    if (req.query.id && post[0]) {
        return { ...post[0]._doc };
    }
    return post;
}

帖子模型:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const schema = new Schema({
    title: { type: String, unique: true, required: true, minlength: 1, maxlength: 64 },
    meta: {
        lang: { type: String, minlength: 2, maxlength: 2, default: 'en' },
        distribution: { type: String, minlength: 1, maxlength: 64, default: 'global' },
        robots: { type: String, minlength: 1, maxlength: 64, default: 'index,follow' },
        author: { type: String, minlength: 1, maxlength: 64 },
        description: { type: String, minlength: 1, maxlength: 256 },
        keywords: { type: String, minlength: 1, maxlength: 256 },
        canonical: { type: String, minlength: 1, maxlength: 512 },
        others: { type: [Schema.Types.Mixed], default: [] },
    },
    coverImage: { type: String, minlength: 1, maxlength: 1024 },
    summary: { type: String, required: true, minlength: 1, maxlength: 1024 },
    body: { type: [Schema.Types.Mixed], default: [] },
    slug: { type: String, unique: true, required: true, minlength: 1, maxlength: 1024 },
    createdDate: { type: Date, required: true, default: Date.now },
    status: { type: Boolean, required: true, default: true },
    categories: [{ type: String, minlength: 1, maxlength: 32 }],
    like: { type: Number, default: 0 },
    dislike: { type: Number, default: 0 },
});

module.exports = mongoose.model('Posts', schema);

如何防止服务崩溃或者我需要修复代码?谢谢。

【问题讨论】:

    标签: node.js mongodb express crash


    【解决方案1】:

    您可以在发送查询之前检查提供的字符串是否为有效的 MongoDB ID:

    var ObjectId = require('mongoose').Types.ObjectId;
    
    async function get(req) {
        
        if (!ObjectId.isValid(req.query.id)){
            throw { status: 400, error: `Invalid ID` }
        }
    
        ...
    }
    

    【讨论】:

      【解决方案2】:
      const post = await Posts.find(findParams, (e, post) => {
          if (e) throw { status: 500, error: `Post not found` };
          return post;
      }).sort({ createdDate: -1 });
      

      这很奇怪。如果您正在使用 await,则不要传递回调。只需尝试/解决这个问题,它就会发现错误。

      【讨论】:

        猜你喜欢
        • 2016-07-28
        • 1970-01-01
        • 2016-09-23
        • 2021-11-19
        • 2018-10-13
        • 1970-01-01
        • 2020-10-06
        • 2015-09-22
        • 1970-01-01
        相关资源
        最近更新 更多