【发布时间】:2021-08-12 14:10:04
【问题描述】:
我想从该设备动态获取 IP 地址(因为它会超时更改),然后使用来自 node.js 服务器的 fetch:https://{ipadress}:5000/users 中的链接从数据库中获取 JSON。但现在它给了我一个错误 Can not use keyword 'await' outside an async function。我不知道如何解决它。
函数publicIP() 是问题所在。
async componentDidMount() {
this.drawOnCanvas();
var ipAdress = '';
publicIP()
.then(ip => {
console.log(ip);
const url = "http://" + ip + ":5000/user";
const response = await fetch(url);
const data = await response.json();
this.setState({userData: data});
console.log(this.state.userData);
// '47.122.71.234'
})
.catch(error => {
console.log(error);
// 'Unable to get IP address.'
});
【问题讨论】:
标签: node.js reactjs react-native async-await