繁体   English   中英

react-native异步获取,未定义错误

[英]react-native async fetch, undefined error

调用GetByUsername时,执行跳转到catch块,但err未定义。 api之所以有效,是因为等效的promise样式代码.then()。then()。done()可以工作,但是我想以这种异步/等待样式更好地编写。 我该如何调试?

var cli = {
    GetByUsername: async function(username) {
       try {
         let resposne = await fetch(`http://api.example.com?username=${username}`);
         return response;
       } catch(err) {
         debugger;
       }
    }
}

编辑:通过查看react-native的package.json ,似乎使用的获取实现是node-fetchbabeljs作为编译器。

尝试这个:

 const response = await fetch(`http://api.example.com?username=${username}`); const jsonData = await response.json(); // then you can use jsonData as you want 

我发现问题是拼写错误的变量,因此我返回了一个不存在的变量,该变量导致执行以未定义的错误在catch块中结束。

暂无
暂无

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

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