.map() , Undefined 不是 React Native 中的函数

.map() , Undefined is not a function in React Native

所以我调用了一个 API 并用它来设置状态。

我的状态:

  state = {
   candlesticks: []
};

我的 API 调用和承诺函数:

componentDidMount() {
    axios
      .get(
    "apiurl"
      )
      .then(data => {
        let mappedData = data.map((record) => {record.date *= 1000}); //getting the error here with the map()function
        this.setState({
          candlesticks: mappedData
        });
      });
  }

我尝试了不同的代码变体,但仍然出现此错误。 我正在使用 Expo 框架。

undefined is not a function (evaluating 'data.map(function (record){record.date *= 1000})')

Axios returns 一个 response 对象。在响应中,您有一个数据 属性 和您的数据。
所以你需要做的是:

componentDidMount() {
    axios.get("apiurl")
    .then(response => {
        let mappedData = response.data.map((record) => {record.date *= 1000}); //getting the error here with the map()function
        this.setState({
            candlesticks: mappedData
        });
    });
}

编辑

axios 似乎不太可能有问题,但为了排除它,尝试使用内置的 fetch 模块并查看数据是否仍然为空。

componentDidMount() {
    fetch("apiurl")
    .then(response => {
        return response.json();
     })
    .then(data => {
        let mappedData = data.map((record) => {record.date *= 1000});
        this.setState({
            candlesticks: mappedData
        });
    });
}

如果为空,则问题出在您的服务器上。