简体   繁体   English

NodeJS并发请求响应是混合的

[英]NodeJS concurrent requests response is mixed

When concurrent requests are made the response data is mixed.当发出并发请求时,响应数据是混合的。

The route controller injects the dependencies to the use-case functions.路由 controller 将依赖项注入用例函数。

Route controller路线 controller

const fromAdaptReq = require('../../utils/adapt-req');
...other imports

router.get('/v1/getList', async (req, res, next) => {
    try {
        const request = {
                      locals: res.locals,
                      lang: res.locals.lang
                     };

        const userCase = new fromUsersCase
            .user
            .user
            .getList({
                createError: customError.CustomError,
                translate,
                request: request,
                db: fromDB.database,
            });
        const result = await userCase.execute();
        return res.status(200).json({
            msg: result.msg,
            data: result.data,
            error: false
        });
    } catch (error) {
        console.log(error)
        next(error);
    }
}

Search listing function is used to call the query function搜索列表 function 用于调用查询 function

search listing function搜索列表 function

exports.getList = function ({ createError, translate, request, db }) {
    return Object.freeze({
        execute: async () => {
            try {
                const userId = parseInt(request.locals.userId);

                const searchList= await fromServices
                                        .getPhonebook(request.lang, createError, translate, { userId, db });

                return {
                    msg: 'List',
                    data: { list: searchList}
                }
            } catch (error) {
                if (error instanceof createError) {
                    throw error;
                }
                console.log(error);
                throw new Error(error.message);
            }
        }
    })
}

I found that I was using the global variable to store the user-id inside the fromService function which was performing the query based on user id to generate the list.我发现我正在使用全局变量将用户 ID 存储在fromService function 中,它根据用户 ID 执行查询以生成列表。

Changing blocked scope fixed the error Thank you更改阻止 scope 修复了错误谢谢

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

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