最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - typeError _ref is undefined - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

It 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);});
}
发布评论

评论列表(0)

  1. 暂无评论