简体   繁体   English

Node/Express API 当获取 mutlipel 请求数据时混淆并出现不正确的结果

[英]Node/Express API when getting mutlipel requests data gets mixed up and incorrect results occur

I have a nodejs/express backend api that when I get hundreds of requests within seconds I start to get mixed results where data is crossed between requests and leads to unexpected and incorrected results.我有一个 nodejs/express 后端 api,当我在几秒钟内收到数百个请求时,我开始得到混合结果,其中数据在请求之间交叉并导致意外和不正确的结果。 Can someone point me to where I am incorrectly defining variables that when I get one of the await functions it causes the other variables to be overwritten with the next requests data and causing my issues.有人能指出我在哪里错误地定义了变量,当我得到一个等待函数时,它会导致其他变量被下一个请求数据覆盖并导致我的问题。

router.post('/autoscan3', async(req,res,next) => {

  let authrule = "autoscan"

  console.log(req.body.companyDomain  + "   " + req.body.user + " for folder: " + req.body.folderid)


  let totalscans = 0;

  let client = await getDB();
  let authorizationResults = await checkAuthorization(client, req.body.companyDomain);

  const featureSet = authorizationResults.features;
  let company = authorizationResults.company;
  const clientid = authorizationResults.client_id;
  const clientsecret = authorizationResults.secret;

  let tenantid = await getTenantID(req.body.companyDomain, client);

  let token = await msgraphservices.getClientCredentialsAccessToken(tenantid, clientid, clientsecret)
  let scanResults = []
  let folderscans = 0;
  try{
    for(let a = 0; a < req.body.messages.length; a++){
      let senderAddress = req.body.messages[a].senderAddress;
      let emailBody = req.body.messages[a].emailBody;
      let messageid = req.body.messages[a].messageid;
      let receivedDateTime = req.body.messages[a].receivedDateTime;
      let user = req.body.messages[a].recieverAddress;
      let subject = req.body.messages[a].subject;
      let attachments = req.body.messages[a].attachments;
      let links = req.body.messages[a].emailBodyLinks;
      let headers = req.body.messages[a].headers
      let knownaddress
      if (senderAddress.includes(req.body.companyDomain)) {
        knownaddress = 10
      } else {
        knownaddress = await searchForSender(client, senderAddress, req.body.user, token, "");}

      let assessmentResults = await assessment(
            messageid,
            emailBody,
            senderAddress,
            user,
            subject,
            receivedDateTime,
            company,
            attachments,
            links,
            req.body.companyDomain,
            client,
            headers,
            knownaddress,
            featureSet,
            authrule
          )
      console.log('adding to folderscans')
      folderscans++
      try {
        await msgraphservices.updateUserCategory2(messageid, req.body.user, req.body.folderid, assessmentResults.riskFactor,token)
      }
      catch(e) {
        console.log(`error on category tag for ${messageid} with user ${req.body.user}`);
        console.log(e);
        }
    }
      console.log(`folder scans ${folderscans}`);
      totalscans = totalscans + folderscans
    return res.status(200).json({status:"success", totalscans:totalscans});
  }
  catch(e) {
    console.log(`error while trying to loop for user ${req.body.user}`)
    console.log(e)
    logapierror(e.stack, req.body.user, req.body)
    return res.status(200).json({status:'error', totalscans:totalscans, error:e});
  }
});

It's very likely let client = await getDB();很有可能let client = await getDB();

If multiple requests use the same snapshot of the database but don't know about each other, it's very likely that they're overwriting each other's data.如果多个请求使用相同的数据库快照但彼此不了解,它们很可能会覆盖彼此的数据。

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

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