同样的代码适用于其他API,但不适用于这个API。

huangapple go评论74阅读模式
英文:

same code working for other API but not for this API

问题

以下是您提供的代码的中文翻译:

相同的代码在另一个API上运行正常,但在这个API上不起作用。

如果我漏掉了什么,请告诉我。我无法解决这个问题,并收到以下错误消息:

`items.map不是一个函数`

```jsx
import React from "react";

class Statelist extends React.Component {
  // 构造函数
  constructor(props) {
    super(props);

    this.state = {
      items: [],
      DataisLoaded: false
    };	
  }

  componentDidMount() {
    const apiUrl = "https://newsapi.org/v2/everything?q=tesla&from=2023-04-10&sortBy=publishedAt&apiKey=ad1b44319c0d4252ab7dcbfb0b2a4d9c";

    fetch(apiUrl)
      .then((res) => res.json())
      .then((json) => {
        this.setState({
          items: json,
          DataisLoaded: true
        });
      })
  }

  render() {
    const { DataisLoaded, items } = this.state;
    if (!DataisLoaded) return
      <div>
        <h1>请等一会儿....</h1>
      </div>;

    return (
      {items.map((item) => (
        <tr>
          <td>{item.id}</td>
          <td>{item.articles[0].author}</td> 
          <td>{item.name}</td>
          <td>{item.email}</td>
        </tr>
      ))}
    );
  }
}

export default Statelist;

希望这有助于您理解代码的内容。如果您需要进一步的帮助,请随时提出。

英文:

The same code working for another API but not for this API

Let me know if I am missing something. I'm not able to fix this issue and get this below error message

items.map is not a function

import React from &quot;react&quot;;

class Statelist extends React.Component {
  // Constructor
  constructor(props) {
    super(props);

    this.state = {
      items: [],
      DataisLoaded: false
    };	
  }

  componentDidMount() {
    const apiUrl = &quot;https://newsapi.org/v2/everything?q=tesla&amp;from=2023-04-10&amp;sortBy=publishedAt&amp;apiKey=ad1b44319c0d4252ab7dcbfb0b2a4d9c&quot;;

    fetch(apiUrl)
      .then((res) =&gt; res.json())
      .then((json) =&gt; {
        this.setState({
          items: json,
          DataisLoaded: true
        });
      })
  }

  render() {
    const { DataisLoaded, items } = this.state;
    if (!DataisLoaded) return
      &lt;div&gt;
        &lt;h1&gt;Pleses wait some time....&lt;/h1&gt;
      &lt;/div&gt;;

    return (
      {items.map((item) =&gt; (
        &lt;tr&gt;
          &lt;td&gt;{item.id}&lt;/td&gt;
          &lt;td&gt;{item.articles[0].author}&lt;/td&gt; 
          &lt;td&gt;{item.name}&lt;/td&gt;
          &lt;td&gt;{item.email}&lt;/td&gt;
        &lt;/tr&gt;
      ))}
    );
  }
}

export default Statelist;

答案1

得分: 0

> items.map 不是一个函数

那么 items 不是一个数组。那它是什么呢?

它在这里被初始化为一个数组:

this.state = {
items: [],
DataisLoaded: false
};

但后来在这里被更新:

this.setState({
items: json,
DataisLoaded: true
});

这意味着 json 不是一个数组。那它是什么呢?

访问你代码中的 API URL,我看到的数据开头是:

{"status":"ok","totalResults":13394,"articles":[

(以此类推)

嗯,这不是一个数组。这是一个对象。也许你想要的是对象上的顶级 articles 属性,它包含一个数组吗?:

this.setState({
items: json.articles,
DataisLoaded: true
});

我不能确定,但你可以。你可以检查从 API 收到的数据结构,决定你想从那个结构中获取什么数据,以及如何使用那些数据。

至于 错误本身,你不能在一个对象上调用 .map(),只能在一个数组上调用。

英文:

> items.map is not a function

Then items is not an array. So what is it?

It's initialized as an array here:

this.state = {
items: [],
DataisLoaded: false
};

But later is updated here:

this.setState({
items: json,
DataisLoaded: true
});

Which means json is not an array. So what is it?

Visiting the API URL in your code, I see data starting with:

{&quot;status&quot;:&quot;ok&quot;,&quot;totalResults&quot;:13394,&quot;articles&quot;:[

(and so on)

Well, that's not an array. It's an object. Perhaps you want that top-level articles property on the object, which contains an array?:

this.setState({
items: json.articles,
DataisLoaded: true
});

I can't know for certain, but you can. You can examine the structure of the data you're receiving from the API and decide what data you want from that structure and how you want to use that data.

As far as the error itself is concerned, you simply can't call .map() on an object, only on an array.

huangapple
  • 本文由 发表于 2023年5月10日 22:11:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定