Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues using { Client } #814

Closed
mperu92 opened this issue Apr 12, 2019 · 17 comments
Closed

Issues using { Client } #814

mperu92 opened this issue Apr 12, 2019 · 17 comments

Comments

@mperu92
Copy link

mperu92 commented Apr 12, 2019

Hi, i'm trying to use the es client with Es6 + React, for connect my app into the elastic cloud using the code from the examples:
import { Client } from '@elastic/elasticsearch'; const client = new Client({ cloud: { id: 'Cloud-Name:Cloud-Id', username: 'elastic', password: 'my-password' } });

But when i launch the program it takes me this error

[ConnectionPool.js:375] Uncaught TypeError: URL is not a constructor

what could be the issue?

@delvedor
Copy link
Member

Hello!
Are you using the client in the browser?

@mperu92
Copy link
Author

mperu92 commented Apr 12, 2019

yes, i'im

Hello!
Are you using the client in the browser?

Yes @delvedor, i'm using React client-side
(are you italian? Really? :))

@delvedor
Copy link
Member

Yes @delvedor, i'm using React client-side

This client is not intended to be used in a browser environment.
Contact Elasticseach directly from the browser exposes you to many security risks and we do not support that.

(are you italian? Really? :))

Yes.

@mperu92
Copy link
Author

mperu92 commented Apr 12, 2019

Ah, ok thanks for this information.
I will try other ways.
(i'm tryng to build an Autocomplete widget with React and Elastic Search)

@delvedor
Copy link
Member

(i'm tryng to build an Autocomplete widget with React and Elastic Search)

In this case, I suggest you take a look at elastic/search-ui

If you want to build your own system, I recommend you to put a simple Node.js server (which uses this client) between your React app and Elasticsearch.

@mperu92
Copy link
Author

mperu92 commented Apr 12, 2019

If you want to build your own system, I recommend you to put a simple Node.js server (which uses this client) between your React app and Elasticsearch.

Wow, thank you so much!
There is some documentation about this specific case?

@mperu92
Copy link
Author

mperu92 commented Apr 12, 2019

Because i haven't an Elastic App Search

@mperu92
Copy link
Author

mperu92 commented Apr 12, 2019

Solved really fast working server-side!

@delvedor
Copy link
Member

Hello!
I'm closing this since it seems you have solved your issue. Please open a new issue if you have more questions! :)

@mperu92
Copy link
Author

mperu92 commented Apr 26, 2019 via email

@arunesh90
Copy link

It seems like this issue also occurs in Cloudflare's workers, any way to work around it?

@travis-r6s
Copy link

@arunesh90 I don't suppose you found the issue with this?

@tdossett
Copy link

What was the solution for this issue ?

@travis-r6s
Copy link

@tdossett I couldn't get it to work, so I just used the es rest api with fetch/exios etc.

@tdossett
Copy link

I see i was thinking about doing the same thing since elasticsearch.js using { Client } does not work yet.

THX do you have sample fetch, axios example ?

@travis-r6s
Copy link

Pulled from a quick workers & graphql demo app:

const endpoint = `${endpoint}/_search`
const credentials = new Buffer.from(`${username}:${password}`).toString('base64')

module.exports = {
  SearchResult: {
    __resolveType(obj, context, info){
      if (obj.first_name) return 'Contact'
      if (obj.name) return 'Company'
      return null
    },
  },
  Query: {
    search: async (parent, { query }, ctx, info) => {
      try {
        const queryBody = {
          multi_match: {
            query,
            fields: ['*_name^3', 'company'],
            fuzziness: 'AUTO'
          }
        }
        const response = await fetch(endpoint, {
          method: 'POST',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'Authorization': `Basic ${credentials}`
          },
          body: JSON.stringify({ query: queryBody })
        })
        const data = await response.json()
        const results = data.hits.hits.map(hit => hit._source)
        return results
      } catch (error) {
        throw new Error(error.message)
      }
    }
  }
}

@tdossett
Copy link

tdossett commented Oct 30, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants