admin管理员组文章数量:1244423
I jump into react with just the basic about javascript, I'm making a query in my server that handle mysql and the connection is fine but the return is where I have a problem,it suppose to return a JSON with the query but I found error typeError _ref is undefined here is the function where I connect to my API
callDB(){
fetch('http://localhost:4000/lista')
.then((response)=>{
response.json()
})
.then(({data})=>{
console.log(data);
})
.catch((err)=>{console.log(err);});
}
In the data part is where it doesn't work any ideas?thanks before anything
I jump into react with just the basic about javascript, I'm making a query in my server that handle mysql and the connection is fine but the return is where I have a problem,it suppose to return a JSON with the query but I found error typeError _ref is undefined here is the function where I connect to my API
callDB(){
fetch('http://localhost:4000/lista')
.then((response)=>{
response.json()
})
.then(({data})=>{
console.log(data);
})
.catch((err)=>{console.log(err);});
}
In the data part is where it doesn't work any ideas?thanks before anything
Share Improve this question edited May 2, 2018 at 8:00 Lokesh Kumar Gaurav 7261 gold badge8 silver badges24 bronze badges asked May 2, 2018 at 0:22 Antonio GonzalezAntonio Gonzalez 1752 gold badges2 silver badges7 bronze badges2 Answers
Reset to default 6It happens when trying to destructure an object from the undefined
value, in your case it's { data }
.
So in this particular case returning response.json() from the previous then() handler helps, but other readers may have this problem in other cases as well, in this case you either need to provide a default value like {}
or explicitly check for undefined
before trying to desctucture
You need to return response.json()
in your Promise handler :
callDB(){
fetch('http://localhost:4000/lista')
.then((response)=>{
return response.json()
})
.then(({data})=>{
console.log(data);
})
.catch((err)=>{console.log(err);});
}
本文标签: javascripttypeError ref is undefinedStack Overflow
版权声明:本文标题:javascript - typeError _ref is undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740126361a2228923.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论