Typescript + React: 获取接口上存在但提示属性不存在的属性

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

Typescript + React: Getting Property Does Not Exist on Interface, when it does

问题

以下是代码部分的翻译:

export interface Root {
  stationId: number;
  results: Results;
}
function App() {
  const [data, setData] = useState<Root[]>([]);

  function getTimes(): Promise<Root[]> {
    return fetch("http://localhost:3000/times/9117")
      .then((res) => res.json())
      .then((data) => data as Root[]);
  }

  useEffect(() => {
    getTimes().then((item) => setData(item));
  }, []);

  console.log(data);
  return <div className="App">{data.stationId}</div>;
}

export default App;
{
  "stationId": 9117,
  "results": {
    "LatestUpdate": "2023-02-09T09:59:29",
    "DataAge": 63,
    "Metros": [
      {...}
    ]
}

请注意,上述代码是原始代码的翻译,不包括问题的回答。如果您有其他需要,请随时提出。

英文:

Working with an API that I have matched the properties to an interface using an auto tool:

export interface Root {
  stationId: number;
  results: Results;
}

For brevity, I won't include the additional results interfaces. I then send the fetch request to get the data, and I am now trying to display this in my JSX elements.

function App() {
  const [data, setData] = useState&lt;Root[]&gt;([]);

  function getTimes(): Promise&lt;Root[]&gt; {
    return fetch(&quot;http://localhost:3000/times/9117&quot;)
      .then((res) =&gt; res.json())
      .then((data) =&gt; data as Root[]);
  }

  useEffect(() =&gt; {
    getTimes().then((item) =&gt; setData(item));
  }, []);

  console.log(data);
  return &lt;div className=&quot;App&quot;&gt;{data.stationId}&lt;/div&gt;;
}

export default App;

The API response looks something like this:

{
  &quot;stationId&quot;: 9117,
  &quot;results&quot;: {
    &quot;LatestUpdate&quot;: &quot;2023-02-09T09:59:29&quot;,
    &quot;DataAge&quot;: 63,
    &quot;Metros&quot;: [
      {...}
     ]
}

I receive an error that says:
>Property 'stationId' does not exist on type 'Root[]'

I am not sure how this is possible since when I hover over stationId it says (property) Root.stationId: number. Any ideas on why it is not recognizing this as a property of the interface? Also, how would I read the properties in the API response like data.results.Metros?

I have tried console.log(data), and it shows the object as expected. I also have tried JSON.stringfy(data) which also works and shows on the page. I am not sure why I can't just read this property and show it in an element.

答案1

得分: 3

以下是翻译好的代码部分:

function App() {
  const [data, setData] = useState<Root | null>(null);

  function getTimes(): Promise<Root> {
    return fetch("http://localhost:3000/times/9117")
      .then((res) => res.json())
      .then((data) => data as Root);
  }

  useEffect(() => {
    getTimes().then((item) => setData(item));
  }, []);

  return <div className="App">{data?.stationId}</div>;
}

export default App;
英文:

There is a misalignment between the data you are getting, which is an object, and how you defined your type, which is an array of that object. You could do so instead:

function App() {
  const [data, setData] = useState&lt;Root | null&gt;(null);

  function getTimes(): Promise&lt;Root&gt; {
    return fetch(&quot;http://localhost:3000/times/9117&quot;)
      .then((res) =&gt; res.json())
      .then((data) =&gt; data as Root);
  }

  useEffect(() =&gt; {
    getTimes().then((item) =&gt; setData(item));
  }, []);

  return &lt;div className=&quot;App&quot;&gt;{data?.stationId}&lt;/div&gt;;
}

export default App;

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

发表评论

匿名网友

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

确定