appbaseio / reactivesearch

Search UI components for React and Vue
https://opensource.appbase.io/reactivesearch
Apache License 2.0
4.89k stars 471 forks source link

Cannot read property '0' of undefined Error with a transformed URL Query #1395

Closed PascalStehling closed 4 years ago

PascalStehling commented 4 years ago

Affected Projects React

Library Version: 3.4.3

Describe the bug If you want to change _msearch to _search as seen in the documentation (https://docs.appbase.io/docs/reactivesearch/v3/overview/reactivebase/#props in transformRequest) it has an Problem with the Structure of the Body, because of the {"preference":"queryResult"}. If you remove that part from the Body it throws the following error TypeError: Cannot read property '0' of undefined at query.js:1 at Array.forEach () at handleResponse (query.js:1) at query.js:1

To Reproduce You can Reproduce the Error in the Following Sandbox in App.js Line 40: https://codesandbox.io/s/gitxplore-app-qmjdn

Expected behavior In My Expactation i Should atleast become an Answer from the Server, which i would log with the help of the transformResponse function, but before the Request is send or at the moment the Result is incoming the Error happens.

bietkul commented 4 years ago

@PascalStehling Can you share a code sandbox link that replicates the bug?

PascalStehling commented 4 years ago

@PascalStehling Can you share a code sandbox link that replicates the bug?

@bietkul See new Error Desciption

bietkul commented 4 years ago

Thanks, @PascalStehling for the description. I got the issue, we'll fix it soon.

kud commented 4 years ago

It's probably something around but if your API response doesn't have

{ responses: [] }

ReactiveList will throw Cannot read property '0' of undefined

I had to add this into my node server:

return res.send({ responses: [body] })

It's certainly a bug I did but ReactiveList should say that the response is not well done, instead of having an error Cannot read property '0' of undefined

kud commented 4 years ago

I'm not sure if I was clear before, but I'm a beginner about Elastic Search as backend dev (I only do the frontend part usually). And I totally forgot to wrap my answer with an object and an attribute called responses. I sent directly the answer of my Elastic Search. And I was surprised that ReactiveSearch throws an not understandable error.

It's not your fault for sure because it's a wrong case, but I think we could be better by notifying the developer that the response is bad instead of crashing.

PascalStehling commented 4 years ago

@kud

Thanks for your Answer. But i didnt quit understand, where I need to add return res.send({ responses: [body] }) into my Node Server. Could you please explain this in a littile bit more Detail?

Thanks for you help.

kud commented 4 years ago

Hey @PascalStehling .

Sorry, I used this issue because I've got quite the same error but for another reason, so what I was saying previously won't help you so much.

However, just a few notes about _msearch and _search.

So it means that for _msearch you will be able to send the object with preferences and the object with the query where in _search you'll be only able to send the query object. What I mean here is for the params part.

For the response, I don't know so much but it could also means that you can only have one object as responses, and not a collection.

You should check that in the documentation of Elastic Search.

My opinion is that ReactiveSearch for the moment is only done for _msearch so you probably should change the server more than the front part.

PascalStehling commented 4 years ago

@kud again thanks for your Answer. The Problem is that I have to to work with a plugin of ES and need to use an variantion of _search.

@bietkul is there a way to work with _search or is @kud right and ReactiveSearch only works with _msearch at the moment?

kud commented 4 years ago

I'm not 100% sure but IMO ReactiveSearch was done at first with _msearch and it's not so easy to switch it to _search. But I prefer to let @bietkul speak about that.

PascalStehling commented 4 years ago

@bietkul if @kud is right, you should change your Documentation in the ReactiveBase part, at TransformRequest, because that (The above example will change the default _msearch request to _search request.) will not work.

kud commented 4 years ago

All I know is _search is in json where _msearch is ndjson. And Reactive Search uses ndjson in payload/params when sending elements in POST, which is not compatible when you've got a server in only json.

bietkul commented 4 years ago

@PascalStehling @kud ReactiveSearch is designed to work with _msearch requests. However, you can change the request using the transformRequest prop in ReactiveBase. For example, if you're changing the URL from _msearch to _search then it'll have issues because both requests return different kinds of responses and ReactiveSearch expects the response for _msearch. I think @PascalStehling can use transformResponse prop in ReactiveBase to tweak the response of _search request to _msearch response. Let me know if that works.

kud commented 4 years ago

@bietkul however, how do you deal with the payload from ReactiveSearch when it sends ndjson / multiple objects?

PascalStehling commented 4 years ago

@bietkul You cant work with transformResponse, because the Error happens befor that function is called. I tried it with logging the Response, but i never got one because of the error.

bietkul commented 4 years ago

@kud We use the _msearch method from appbase-js which accepts an array of objects and transforms it into the ndjson format before making the final request to ES.

kud commented 4 years ago

That's interesting, thank you!

DevMaster921 commented 4 years ago

Hi, @kud Nice to meet you.

I have the same issue with @PascalStehling , so could you let me know how I can fix this issue?

Thanks.