nodejs api如果在循环中击中多个API,就会给出相同的结果

发布于 2025-02-12 04:57:40 字数 1244 浏览 0 评论 0原文

现在在后端(nodejs)中创建的API

从另一个服务器后端

现在从frontend击中该API,或者从循环中点击请求时

击中该API,它给出了上一个请求的结果。假设我们是通过ID获取用户数据,以及其用户类别

以下是从前端或后端中的用户IDS 1,2和3

,我有一个数组,我需要命中以获取

Frontend ARR或其他的 用户详细信息后端= [1,2,3] 考虑另一个后端,

const async = require('async');
const axios = require('axios');
let userIds = [1,2,3]

async.each(userIds , function (item, next) {
          console.log("item which is id", item);              
          ongoingReq(item, next);
        }, function (err) {
          if (err) {
            console.error('Error: ' + err.message);
            return;
          }    
        });

function ongoingReq(item, next) {
  // Configure the request 
  let headers = {
  }
  let url = `url${item}/data`;

  axios.get(url, {headers})
  .then(d=>{  
    console.log(d.data.id, item); //item is 1 or 2 or 3 but d.data.id data always giving the first result which processed by the backend
  })
  .catch(err=>{
    console.log("d err", item, err);
  })
}

然后在后端进行交叉检查,它给出了相同的结果。 因此,即使是可能的nodejs api都会给出其他API数据的结果。

不知道为什么会发生这种情况,而当我一一敲击API或延迟API然后后端正确处理数据。但是,每当请求以循环/许多请求发送时,无论其他用户如何,都会给出另一个用户的结果。

有什么建议吗?

来自Nodejs服务器的问题不在前端或后端服务器中

Created APIS in the backend(nodejs)

Now hitting that API from frontend or from another server backend

When I hit the requests in the loop, it gives the result of the previous request.

Suppose we're fetching user data by id and along with its user categories

Following are the user ids 1,2 and 3

From the frontend or backend, I have an array that I need to hit to fetch the user detail

In frontend arr or another backend= [1,2,3]
Consider another backend

const async = require('async');
const axios = require('axios');
let userIds = [1,2,3]

async.each(userIds , function (item, next) {
          console.log("item which is id", item);              
          ongoingReq(item, next);
        }, function (err) {
          if (err) {
            console.error('Error: ' + err.message);
            return;
          }    
        });

function ongoingReq(item, next) {
  // Configure the request 
  let headers = {
  }
  let url = `url${item}/data`;

  axios.get(url, {headers})
  .then(d=>{  
    console.log(d.data.id, item); //item is 1 or 2 or 3 but d.data.id data always giving the first result which processed by the backend
  })
  .catch(err=>{
    console.log("d err", item, err);
  })
}

Then cross-checked in the backend it gives the same result. So how would even that possible NodeJs API gives the result of the other API data.

Don't know why that happened and when I hit api one by one or delay between the APIs then the backend processes data correctly. But whenever the request is sent in a loop/ so many requests at the same time it gives the result of another user irrespective of another user.

Any suggestions for this?

Problem from Nodejs server coming not in the front end or backend server

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人如玉 2025-02-19 04:57:41

我认为这可能与JavaScript句柄循环的方式有关:

for (var i=0;i<5;i++){
    setTimeout(function(){
        console.log(i);
    }, 1000);
}

//---> Output  5,5,5,5,5

也许尝试:

let data = await Promise.all(
  userIds.map(userId =>
    axios.get(`url${userId}/data`, {headers})
  )
)

I think it could have to do with the way javascript handles loop :

for (var i=0;i<5;i++){
    setTimeout(function(){
        console.log(i);
    }, 1000);
}

//---> Output  5,5,5,5,5

Maybe try :

let data = await Promise.all(
  userIds.map(userId =>
    axios.get(`url${userId}/data`, {headers})
  )
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文