NodeJS 等待JS中的响应状态200

93ze6v8z  于 2022-11-29  发布在  Node.js
关注(0)|答案(1)|浏览(226)

问题如下:我有13个API,必须在页面启动时执行,但服务器没有等待它们的响应,并显示错误500,直到我执行了几次刷新,我得到状态200,但我不希望这样,我不希望客户端多次刷新,直到它看到响应。
我不明白的是,POSTMAN是如何等待响应的,而Web却不这样做的,每次我更改API的参数时,我都会遇到这种情况
When I change the parameters, some are executed but others are not, I realize that react does not expect the promise and displays it even though it does not bring anything
postman if wait for response

我已经使用了节点代码遗留、axios、请求、超时等,但我无法让它等待请求****我已经在Angular 和React中完成了项目,但我无法。
这是我尝试过的最后一个,我认为有13个API

import React, { useEffect }from 'react';
// import DashboardService from "../services/DashboardService";
import axios from "axios";
import ApiConstants from "../constants/ApiConstants";

const DashBoardRequest = axios.create({
    baseURL: ApiConstants.BACKEND_DASHBOARD_API.BASE_API_URL,
    timeout: 30000,
});

const HomePage = () =>{

const filteredHeatmap = () => {
  return new Promise(async () => {
    const params = {
      id_enterprise: "9",
      date_start: "2022-05-12",
      date_end: "2022-07-09",
      age_ini: "20",
      age_end: "60",
      gender_id: "4"
    };
      await DashBoardRequest.get(
      ApiConstants.BACKEND_DASHBOARD_API.GET_FILTERED_HEATMAP,
      {params}
      ).then(
        res =>{
          console.log(res)
        }
      )
  })
}
async function resFilteredHeatmap(){
  await filteredHeatmap()
}

Promise.race([
....
resFilteredHeatmap(),
....
]).then(res=>{
  console.log(res)
})

    return (
    <div>
    hola
    </div>
)

}

export default HomePage;
mnemlml8

mnemlml81#

const filteredBarData = (
    id_enterprise,
    date_start,
    date_end,
    age_ini,
    age_end,
    gender_id,
    brand
  ) => {
  const params = {
    id_enterprise: id_enterprise,
    date_start: date_start,
    date_end: date_end,
    age_ini: age_ini,
    age_end: age_end,
    gender_id: gender_id,
    brand: brand,
  };
 return DashBoardRequest.get(
        ApiConstants.BACKEND_DASHBOARD_API.GET_FILTERED_BAR_DATA,
        {params}
        )
}

async function resFilteredBarData(
  id_enterprise, date_start, 
  date_end, age_ini, age_end, 
  gender_id, brand
){
  await filteredBarData(
    id_enterprise, date_start, 
    date_end, age_ini, age_end, 
    gender_id, brand
  ).then(
    res=>{
      console.log(res)
      console.log(res.status)
      console.log(res.data)
    }
  )
}

resFilteredBarData("9", "2022-03-10", "2022-07-19", "20", "50", "4", "VICTORIA")

相关问题