【问题标题】:LOG {"_U": 0, "_V": 0, "_W": null, "_X": null} inside fetch APILOG {"_U": 0, "_V": 0, "_W": null, "_X": null} 在 fetch API 中
【发布时间】:2021-04-21 04:17:47
【问题描述】:

当我控制台从从后端获取数据的函数返回的数据时出现此错误

{“_U”:0,“_V”:0,“_W”:空,“_X”:空}

下面是代码:

const events_data = [];
function getvals() {

    return fetch('http://PCIP:3000/users/timetable')
        .then((response) => response.json())

        .then((output) => {
            return addData(output, events_data);
        })


        .catch(error => console.log(error))
}

function addData(data, data2) {
    data.map((d) => {
        data2.push({
            title: d.name,
            startTime: genTimeBlock(d.day, d.start_time),
            endTime: genTimeBlock(d.day, d.end_time),
            location: d.location,
            extra_descriptions: [d.extra_descriptions],
        });
    });
}


const data = getvals();
console.log(data); // the error come from here 

我在这里检查了答案,但对我没有任何帮助

fetch API always returns {“_U”: 0, “_V”: 0, “_W”: null, “_X”: null}

How do I access promise callback value outside of the function?

【问题讨论】:

    标签: json react-native fetch-api


    【解决方案1】:

    这是因为 fetch 承诺还没有返回响应,

    有两种方法可以解决这个问题,首先你创建另一个异步函数并使用它来等待响应

    const events_data = [];
    async function getvals() {
    
        return fetch('http://PCIP:3000/users/timetable')
            .then((response) => response.json())
    
            .then((output) => {
                return addData(output, events_data);
            })
    
    
            .catch(error => console.log(error))
    }
    
    function addData(data, data2) {
        data.map((d) => {
            data2.push({
                title: d.name,
                startTime: genTimeBlock(d.day, d.start_time),
                endTime: genTimeBlock(d.day, d.end_time),
                location: d.location,
                extra_descriptions: [d.extra_descriptions],
            });
        });
    }
    
    async function waitForResponse() {
    let resp = await getvals();
    
    return resp;
    }
    
    const data = waitForResponse();
    console.log(data); // the error come from here 
    
    

    另一种方法是使用状态钩子,将返回的 obj 传递给响应时的状态钩子。

    【讨论】:

    • 你能告诉我你想做什么,你需要在类或功能组件中调用它才能使用它,如果你不想切换到功能组件,那么有我们可以做的一件事是 ```` const test = waitForResponse() .then((resp)=>{ // 把剩下的代码放在这里 }); ````
    • 实际上我切换到功能组件,它对我有用,比你好多了
    【解决方案2】:

    API调用函数:

      export const getApplication = async (URL, headers) => {
        let data;
        await fetch.get(URL, headers).then(res => data = res.data).catch(err => err);
        return data;
      }
    

    您可以在导入 API 后从任何地方调用它:

          getApplication(`your url`, {
            headers: {
              Authorization: AUTH_TOKEN,
            },
          }).then(res => console.log(res)).catch(err => console.log(err));
        
    

    【讨论】:

      猜你喜欢
      • 2021-03-02
      • 2022-01-14
      • 2022-01-14
      • 2022-01-05
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      相关资源
      最近更新 更多