Javascript 如何修复来自eslint的此错误?
有没有更好的方法来编写这部分代码?这是可行的,但eslint抱怨说 “resp”已在上层作用域中声明 在这部分Javascript 如何修复来自eslint的此错误?,javascript,eslint,Javascript,Eslint,有没有更好的方法来编写这部分代码?这是可行的,但eslint抱怨说 “resp”已在上层作用域中声明 在这部分 return parseJSON(resp).then((resp) => { throw resp }) 这是全部代码 componentDidMount = async () => { const parseJSON = (resp) => (resp.json ? resp.json() : resp) con
return parseJSON(resp).then((resp) => {
throw resp
})
这是全部代码
componentDidMount = async () => {
const parseJSON = (resp) => (resp.json ? resp.json() : resp)
const checkStatus = (resp) => {
if (resp.status >= 200 && resp.status < 300) {
return resp
}
return parseJSON(resp).then((resp) => {
throw resp
})
}
const headers = {
'Content-Type': 'application/json'
}
try {
const data = await fetch('http://myurl/api', {
method: 'GET',
headers: headers
}).then(checkStatus)
.then(parseJSON)
this.setState({ data })
} catch (error) {
this.setState({ error })
}
}
componentDidMount=async()=>{
const parseJSON=(resp)=>(resp.json?resp.json():resp)
const checkStatus=(resp)=>{
如果(响应状态>=200&响应状态<300){
返回响应
}
返回parseJSON(resp)。然后((resp)=>{
掷骰子
})
}
常量头={
“内容类型”:“应用程序/json”
}
试一试{
常量数据=等待获取('http://myurl/api', {
方法:“GET”,
标题:标题
}).然后(检查状态)
.then(解析JSON)
this.setState({data})
}捕获(错误){
this.setState({error})
}
}
您在同一范围内的以下行中有两次变量名resp
:
const checkStatus=(resp)=>{
然后((resp)=>{
更改resp.中的一个。然后((resp)
就是问题所在。在父箭头函数中有相同的命名变量。