Javascript MEAN Node JS请求在多个请求中进行交互

Javascript MEAN Node JS请求在多个请求中进行交互,javascript,node.js,express,mean-stack,Javascript,Node.js,Express,Mean Stack,我有一个普通的应用程序,可以很好地处理单个请求,比如调用/api/products?pid=500。但我最近发现,在“突发”请求时(我正在用post数据批量更新大约50个products=50个请求/api/products?pid=500***550),req.body有时会获得一个新的即将到来的请求的值 front应用程序在选定产品的foreach中进行调用: ds.forEach((d, key) => { this.ApiCall.setData('product

我有一个普通的应用程序,可以很好地处理单个请求,比如调用
/api/products?pid=500
。但我最近发现,在“突发”请求时(我正在用post数据批量更新大约50个products=50个请求
/api/products?pid=500
***550),req.body有时会获得一个新的即将到来的请求的值

front应用程序在选定产品的foreach中进行调用:

 ds.forEach((d, key) => {
        this.ApiCall.setData('products', { action: 'send-product', data: d })
            .subscribe((result) => {
                //we have results
            });
        });
    //setData makes a http.post().map
Back app/mean分析帖子,尝试合成代码:

router.route('/')
.post(function (req, response) {
    if(req.body.data){
        var obj = { id: req.body.data.product_id }
        if(req.body.data.linked_products){
            req.body.data.linked_products.forEach(function(entry) {
                obj.linked = entry; //more ifs
            });
        }
        var async = require('async');
        async.series({
            q2: function(cb){
                queryProducts.findOne({id: req.body.data.product_id, null).exec(cb);
            },
            q3: function(cb){
                queryCategories.findOne({id: req.body.data.category_id, null).exec(cb);
            }
          }, function(err, qResults){

            var alreadysent = false;
            if (qResults.q3) qResults.q3.logs.forEach(function(entry) {
                if(entry.sent){
                    alreadysent = true;
                }
            });
            //more ifs
            qResults.q3.external_codes.forEach(function(entry) {
                obj.external_code = entry;//more ifs
            });
            if(req.body.data.price < 0){
                response.json({message: "Negative price didn't sent"});
                return;
            }
            if(qResults.q2.status=="inactive"){
                response.json({message: "Inactive didn't sent"});
                return;
            }
            req.body.data.campaigns(function(entry) {
                obj.price_offers = entry;//more ifs
            });
            //more ifs and foreach similar
            queryProducts.update({id: req.body.data.id}, {$push: { synced_products: obj }}, function (err, result) {
                //HERE I found req.body.data with values of a future request

                if(!err)
                    response.json({message: "Sent"});
                return;
            });
        });
    }
});
module.exports = router;
具有不同的计时,但是调用最后一个req.body的请求(pid=501)如何可能具有新req(pid=503)的req.body值? 有什么办法可以避免吗?将async放在post后面的第一位,或者

var reqbody = req.body 

谢谢

我认为这是由于
异步
模块初始化造成的。引自:

缓存

模块在第一次加载后被缓存。这意味着(除其他外)每个对require('foo')的调用都将返回完全相同的对象,前提是它将解析为相同的文件

多次调用require('foo')可能不会导致多次执行模块代码。这是一个重要特征。使用它,可以返回“部分完成”的对象,从而允许加载可传递的依赖项,即使它们会导致循环

要让模块多次执行代码,请导出一个函数并调用该函数


当突发请求导致执行重叠时,您将有两个(或更多)使用被“并发”修改的
async
变量。我建议使用某种互斥来控制对
async
变量的访问。

请用演示该问题的示例更新您的问题。引用的代码(如果我们假设在
post
调用中缺少close paren和semi)在请求之间的数据泄漏方面不应该有任何问题,因此在其他地方发生了一些事情。@t.J.Crowder抱歉,更新了代码,谢谢!尝试使用
let
而不是
var
var reqbody = req.body