【问题标题】:Get object array from axios and using return value call another axios function and append the output to first result从 axios 获取对象数组并使用返回值调用另一个 axios 函数并将输出附加到第一个结果
【发布时间】:2022-01-14 04:06:46
【问题描述】:

常量请求 = { 标题:{ “接受”:“申请”, “内容类型”:“应用程序/json”, }, 方法:“获取”, 网址:'', } 任意;

        const result = await axios(request);

        result.data.data.map(async (ad: any) => {
            const request2 = {
                headers: {
                    "Accept": "application",
                    "Content-Type": "application/json",
                },
                method: "GET",
                url: '',
            } as any;

            const result2 = await axios(request2);
            ad.preview = result2.data.data[0].body;
        })

        return result.data.data;

没有附加预览密钥

【问题讨论】:

    标签: node.js reactjs async-await axios


    【解决方案1】:

    问题是返回语句在异步映射回调完成之前运行。由于您实际上并没有在地图中返回任何内容,因此使用它并没有多大意义。只需使用for..of 循环:

    const result = await axios(request);
    
    for (const ad of result.data.data) {
      const request2 = {...}
      const result2 = await axios(request2);
      ad.preview = result2.data.data[0].body;
    }
    
    return result.data.data;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-07
      • 2021-04-24
      • 2021-12-26
      • 1970-01-01
      • 2019-11-26
      • 2022-07-06
      • 2017-09-13
      • 2020-09-10
      相关资源
      最近更新 更多