Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获得反应列表中的所有结果_Javascript_Reactjs_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_React Native_Reactivesearch - Fatal编程技术网 elasticsearch,react-native,reactivesearch,Javascript,Reactjs,elasticsearch,React Native,Reactivesearch" /> elasticsearch,react-native,reactivesearch,Javascript,Reactjs,elasticsearch,React Native,Reactivesearch" />

Javascript 如何获得反应列表中的所有结果

Javascript 如何获得反应列表中的所有结果,javascript,reactjs,elasticsearch,react-native,reactivesearch,Javascript,Reactjs,elasticsearch,React Native,Reactivesearch,我尝试在所有数据上使用它,但它不起作用,因为我只能得到“size”参数中所述的结果数。这里是我代码的一部分 <ReactiveList componentId="results" dataField="original_title" size={1} showResultStats={true} pagination={true} react={{ and: "searchbox" }} onAllData={this.onAllData} />

我尝试在所有数据上使用它,但它不起作用,因为我只能得到“size”参数中所述的结果数。这里是我代码的一部分

<ReactiveList
  componentId="results"
  dataField="original_title"
  size={1}
  showResultStats={true}
  pagination={true}
  react={{
    and: "searchbox"
  }}
  onAllData={this.onAllData}
/>

onAllData(shops) {
  let result = null;
  if (shops !== null) {
    console.log(shops.length);
    result = shops.map((marker) => (
      <ListItem>
        <Thumbnail square size={80} source={{ uri: 'https://dummyimage.com/80x80/000/fff' }} />
        <Body>
        <Text>{marker.name}</Text>
        <Text note>{marker.dis}</Text>
        </Body>
      </ListItem>
    ))
    return result;
  }
}

ONALDATA(商店){
设result=null;
如果(商店!==null){
控制台。日志(商店。长度);
结果=shops.map((标记)=>(
{marker.name}
{marker.dis}
))
返回结果;
}
}

您缺少一个标识唯一组件的键,还需要将元素包装到函数中。如果函数被正确调用,并且数组中有元素,这应该可以工作

onAllData(shops) {
 let result = null;
 if (shops !== null) {
   console.log(shops.length);
   result = shops.map((marker,index) => { return (
      <ListItem key={index}>
        <Thumbnail square size={80} source={{ uri: 'https://dummyimage.com/80x80/000/fff' }} />
        <Body>
          <Text>{marker.name}</Text>
          <Text note>{marker.dis}</Text>
        </Body>
     </ListItem>
    )})
   return result;
}
onAllData(商店){
设result=null;
如果(商店!==null){
控制台。日志(商店。长度);
结果=shops.map((标记,索引)=>{return(
{marker.name}
{marker.dis}
)})
返回结果;
}

有几种方法可以解决此问题:

以无限负载呈现所有结果
onaldata
将给出
size
属性中指定的结果数。通常,设置非常高的
size
不是一个好主意,因为获取和呈现结果会花费更多的时间。一个好的替代方法是通过将
分页
属性设置为
false并在
size
prop中设置一个值,告诉组件到达列表末尾时要获取多少结果

使用scroll API获取所有结果 太长,读不下去了 注意

此答案使用reactivesearch for web(用于演示),但您可以在reactivesearch native中使用相同的道具,因为API是相同的

如果您只对呈现结果感兴趣,则上述方法可以正常工作。但如果您希望获取当前查询的所有结果,则可以使用Elasticsearch中的API。您可以使用
ReactiveList
获取当前查询,然后将其与滚动API一起使用

为此,您可以使用
ReactiveList
上的
onQueryChange
属性:

首先在
ReactiveList
上指定一个
onQueryChange
属性,该属性接收上一个和当前查询作为参数:

onQueryChange={(prev, next) => ...}
每次更改查询时都会调用此函数,以便您可以编写逻辑,在需要时获取当前查询的点击(在
下一个
参数中接收)

scroll API调用以以下格式返回结果:

{
  "_scroll_id": "DnF1ZXJ5VGhlbkZldGNoAgAAAAClGlY4FlotbmJJZXA0U09lMlZFMUNyQ3M2M0EAAAAApRpWORZaLW5iSWVwNFNPZTJWRTFDckNzNjNB",
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 2,
    "successful": 2,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 9407,
    "max_score": 1,
    "hits": [
      {
        "_index": "good-books-ds",
        "_type": "good-books-ds",
        "_id": "5676",
        "_score": 1,
        "_source": {
          "authors": "Arthur C. Clarke, Gentry Lee",
          "average_rating": 3.76,
          "average_rating_rounded": 4,
          "books_count": 48,
          "id": 5676,
          "image": "https://images.gr-assets.com/books/1375814957l/112518.jpg",
          "image_medium": "https://images.gr-assets.com/books/1375814957m/112518.jpg",
          "isbn": "1857230213",
          "language_code": "eng",
          "original_publication_year": 1991,
          "original_series": "Rama",
          "original_title": "The Garden of Rama (Rama, #3)",
          "ratings_count": 16389,
          "title": "The Garden of Rama (Rama, #3)"
        }
      },
      {
        "_index": "good-books-ds",
        "_type": "good-books-ds",
        "_id": "5681",
        "_score": 1,
        "_source": {
          "authors": "Darren Shan",
          "average_rating": 4.22,
          "average_rating_rounded": 4,
          "books_count": 52,
          "id": 5681,
          "image": "https://s.gr-assets.com/assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png",
          "image_medium": "https://s.gr-assets.com/assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png",
          "isbn": "",
          "language_code": "",
          "original_publication_year": 2003,
          "original_series": "Cirque Du Freak",
          "original_title": "Killers of the Dawn (Cirque Du Freak, #9)",
          "ratings_count": 18194,
          "title": "Killers of the Dawn (Cirque Du Freak, #9)"
        }
      },
      {
        "_index": "good-books-ds",
        "_type": "good-books-ds",
        "_id": "5683",
        "_score": 1,
        "_source": {
          "authors": "Laura Joffe Numeroff, Felicia Bond",
          "average_rating": 4.16,
          "average_rating_rounded": 4,
          "books_count": 13,
          "id": 5683,
          "image": "https://s.gr-assets.com/assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png",
          "image_medium": "https://s.gr-assets.com/assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png",
          "isbn": "60278684",
          "language_code": "",
          "original_publication_year": 2000,
          "original_series": "",
          "original_title": "If You Take a Mouse to the Movies",
          "ratings_count": 17938,
          "title": "If You Take a Mouse to the Movies"
        }
      },
      {
        "_index": "good-books-ds",
        "_type": "good-books-ds",
        "_id": "5685",
        "_score": 1,
        "_source": {
          "authors": "Orson Scott Card, James Cameron",
          "average_rating": 4.06,
          "average_rating_rounded": 4,
          "books_count": 15,
          "id": 5685,
          "image": "https://images.gr-assets.com/books/1225165505l/40289.jpg",
          "image_medium": "https://images.gr-assets.com/books/1225165505m/40289.jpg",
          "isbn": "99690608",
          "language_code": "eng",
          "original_publication_year": 1989,
          "original_series": "",
          "original_title": "The Abyss",
          "ratings_count": 16318,
          "title": "The Abyss"
        }
      },
      {
        "_index": "good-books-ds",
        "_type": "good-books-ds",
        "_id": "5687",
        "_score": 1,
        "_source": {
          "authors": "Katarina Bivald, Alice Menzies",
          "average_rating": 3.56,
          "average_rating_rounded": 4,
          "books_count": 63,
          "id": 5687,
          "image": "https://images.gr-assets.com/books/1452107441l/25573977.jpg",
          "image_medium": "https://images.gr-assets.com/books/1452107441m/25573977.jpg",
          "isbn": "149262344X",
          "language_code": "eng",
          "original_publication_year": 2013,
          "original_series": "",
          "original_title": "Läsarna i Broken Wheel rekommenderar",
          "ratings_count": 14571,
          "title": "The Readers of Broken Wheel Recommend"
        }
      }
    ]
  }
}
作为
\u scroll\u id
接收的值可以传递给scroll API以获取下一组结果,依此类推,直到命中数为零

注意

如果您的群集有大量数据,则在每次查询更改时运行此逻辑以获取所有结果不是一个好主意。您可以添加一个条件来限制获取的结果数,或者将当前查询存储在状态中,并且仅在需要时获取所有结果

下面是一个关于如何使用
ReactiveList
实现此功能的示例。在该示例中,每次查询更改时,我都会获取结果,但您可以修改它以有条件地获取结果:

渲染
函数中:

<ReactiveList
  ...
  size={10}
  onQueryChange={this.handleQueryChange}
/>
它使用两个函数来获取最初和以后使用的
scroll\u id
的结果。这两个函数的端点都不同,您可以在中找到。下面是第一个
fetchResults
的外观:

fetchResults = (query, api) => {
    return fetch(api, {
      method: "POST",
      headers: {
        "content-type": "application/json",
        Authorization: `Basic ${btoa(credentials)}`
      },
      body: JSON.stringify(query)
    })
      .then(res => res.json())
      .catch(err => console.error(err));
};
fetchScrollResults
将使用scroll API获取结果,直到获得的点击数为0

fetchScrollResults = async query => {
    const res = await this.fetchResults(query, scrollUrl);
    const { hits } = res.hits;
    if (hits.length) {
        return [
            ...hits,
            ...(await this.fetchScrollResults({
                scroll: "1m",
                scroll_id: res._scroll_id
            }))
        ];
    }
    return [];
};

选中,结果将显示在控制台中。

谢谢您的回答。在我从reactiveList获得所有结果后如何。如果我想将其保存到状态(最多20个结果),我在哪里可以调用setState函数?目前,我在渲染中调用的函数中设置状态。因此,我将面临设置状态的无限循环。我如何解决它…?您可以将它们设置为我已在
handleQueryChange
函数中注销的状态。此外,如果您最多只对20个结果感兴趣,您甚至不需要滚动API。只需使用
fetchResults
方法,在收到的查询中添加
20
size
键作为
下一个
参数。如果我有像Z8qef2A4rR0:de9wfqqefweb4-84a0-40a5-9937-b54ce843356这样的凭证,我应该在
Basic${btoa(credentials)}
中键入什么你必须把整个东西作为凭证放进去。检查我正在做的演示链接。
fetchScrollResults = async query => {
    const res = await this.fetchResults(query, scrollUrl);
    const { hits } = res.hits;
    if (hits.length) {
        return [
            ...hits,
            ...(await this.fetchScrollResults({
                scroll: "1m",
                scroll_id: res._scroll_id
            }))
        ];
    }
    return [];
};