简体   繁体   English

邮递员在发出 node.js 请求时挂起

[英]Postman hangs while making a node.js requests

I'm using postman to make a request to my node.js API, to which I'm using my orders route to send a request following my order controller.我正在使用邮递员向我的 node.js API 发出请求,我正在使用我的订单路由向我的订单控制器发送请求。 Whatever requisition I make using my orders route my requisition hangs and instead of receiving either a 400 or a 500 response, my postman gets stuck on loading screen and it times out after some time.无论我使用我的订单路线进行什么请求,我的请求都会挂起,而不是收到 400 或 500 响应,我的邮递员卡在加载屏幕上,一段时间后超时。 I tried alternatively using the google chrome extension, same results.我尝试使用谷歌浏览器扩展程序,结果相同。 After the requisition is unsuccessful I receive the following message申请不成功后,我收到以下消息信息

I was wondering if it has something to do with how I did the routing in my backend, but I am unsure of this.我想知道这是否与我在后端进行路由的方式有关,但我不确定这一点。 Hence, here's my code:因此,这是我的代码:

model.js:模型.js:

'use strict';

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

const schema = new Schema({
    customer: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Customer'
    },
    number: {
        type: String,
        required: true
    },
    createDate: {
        type: Date,
        required: true,
        default: Date.now
    },
    status: {
        type: String,
        required: true,
        enum: ['created', 'done'],
        default: 'created'
    },
    items: [{
        quantity: {
            type: Number,
            required: true,
            default: 1
        },
        price: {
            type: Number,
            required: true
        },
        product: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Product'
        }
    }],
});

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

post method in my order-controller.js:我的 order-controller.js 中的 post 方法:

exports.post = async(req, res, next) => {
    try {
        const token = req.body.token || req.query.token || req.headers['x-access-token'];
        const data = await authService.decodeToken(token);

        await repository.create({
            customer: data.id,
            number: guid.raw().substring(0, 6),
            items: req.body.items
        });
        res.status(201).send({
            message: 'Order registered with success!'
        });
    } catch (e) {
        console.log(e);
        res.status(500).send({
            message: 'Fail to process your requisition'
        });
    }
};

order-repository.js:订单存储库.js:

'use strict';
const mongoose = require('mongoose');
const Order = mongoose.model('Order');

exports.get = async(data) => {
    var res = await Order
        .find({}, 'number status customer items')
        .populate('customer', 'name')
        .populate('items.product', 'title');
    return res;
}

exports.create = async(data) => {
    var order = new Order(data);
    await order.save();
}

order-route.js:订单-route.js:

'use strict';

const express = require('express');
const router = express.Router();
const controller = require('../controllers/order-controller');
const authService = require('../services/auth-service');

router.get('/', authService.authorize, controller.get);
router.post('/', authService.authorize, controller.post);

module.exports = router;

This can happen if you are debugging your nodejs server and it's caught on a breakpoint.如果您正在调试 nodejs 服务器并且它在断点处被捕获,则可能会发生这种情况。 If so, the response will hang.如果是这样,响应将挂起。 To fix it, just disable that breakpoint, restart your nodejs server, and issue the request again with Postman.要修复它,只需禁用该断点,重新启动 nodejs 服务器,然后再次使用 Postman 发出请求。

Maybe you are using a GET request in the postman instead of POST request.也许您在邮递员中使用 GET 请求而不是 POST 请求。 Make sure that they comply with the type.确保它们符合类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM