简体   繁体   English

如何在反应中使用javascipt获取条件数据?

[英]how to fetch data with a condition using javascipt in react?

I want to load only specific number of rows where the condition satisfies.我只想加载条件满足的特定行数。

However the following code fetches all the 25 rows and condition is not working.但是,以下代码获取所有 25 行并且条件不起作用。

Please someone help.请有人帮忙。

 loadCommentsFromServer = () => {
       fetch('/api/comments/', {
       method: 'GET',
       author: 'Dani'
   })
      .then(data => data.json())
      .then((res) => {
        if (!res.success) this.setState({ error: res.error });
        else this.setState({ data: res.data });
      });
  }

I am very new to javascript and reactjs, any help will save my weekend.我对 javascript 和 reactjs 很陌生,任何帮助都可以挽救我的周末。

You don't have access to the response in your second callback there.您无权访问那里的第二个回调中的响应。

loadCommentsFromServer = () => {
  return fetch('/api/comments/', {
    method: 'GET',
    author: 'Dani'
  })
    .then((res) => {
      if (!res.success) this.setState({ error: res.error });
      else res.json().then(data => this.setState({ data }));
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM