无法破坏属性

时间:2019-08-07 20:01:01

标签: javascript jquery

  

TypeError:无法解构'undefined'的属性results或   '空值'。       在displayCartTotal

 const displayCartTotal = ({results}) => {

  };

  const fetchBill = () => {
    const apiHost = 'https://randomapi.com/api';
    const apiKey = '006b08a801d82d0c9824dcfdfdfa3b3c';
    const apiEndpoint = `${apiHost}/${apiKey}`;

    fetch(apiEndpoint) 
    .then( response => {
        return response.json();
    })
    .then(results => {
        console.log(results.results)
        displayCartTotal();
     })
     .catch(err => console.log(err));
  };

2 个答案:

答案 0 :(得分:1)

收到错误是因为您没有像results那样将displayCartTotal传递到displayCartTotal(results)

答案 1 :(得分:0)

您正在调用没有参数的displayCartTotal(),但是它需要一个对象。参见下面的注释行:

const displayCartTotal = ({results}) => {

  };

  const fetchBill = () => {
    const apiHost = 'https://randomapi.com/api';
    const apiKey = '006b08a801d82d0c9824dcfdfdfa3b3c';
    const apiEndpoint = `${apiHost}/${apiKey}`;

    fetch(apiEndpoint) 
    .then( response => {
        return response.json();
    })
    .then(results => {
        console.log(results.results)
        displayCartTotal(); //<--- this is the offending line
     })
     .catch(err => console.log(err));
  };

您应将结果作为这样的参数传递:displayCartTotal(results)

希望能为您解决:)

相关问题