1

I'm using elasticsearchjs in a small project using electron. But I somehow encounter something strange that blocks me.

In Electron, I have a button, that triggers a function on a click:

<button onclick="someFunction()">Click Me</button>

And the following Javascript:

import elasticsearch from 'elasticsearch'

function someFunction () {
    console.log('hello world')

    let es = new elasticsearch.Client({
        host: 'http://127.0.0.1:9200',
        log: 'trace'
    })

    es.ping().then(response => {
      console.log(response) // Does not enter, promise is rejected
    })
}

I can see the hello world output, but somehow the elastic search functionalities are not working, I get a timeout error...

But if I double the call to the function, and add an asynchronous call to the elasticsearch API, it works and I enter into the two then() calls:

import elasticsearch from 'elasticsearch'

function someFunction () {
    console.log('hello world')

    let es = new elasticsearch.Client({
        host: 'http://127.0.0.1:9200',
        log: 'trace'
    })

    es.ping().then(response => {
      console.log(response) // promise resolves once the second one is resolved
    })

    setTimeout(() => {
        es.ping().then(response => {
            console.log(response) // resolves
        })
    }, 500)
}

And if I only put the setTimeout() function, it doesn't work either, it's like I need to call the function twice to get it to work.

I tried on a real node script and the code works well:

let elasticsearch = require('elasticsearch')

let es = new elasticsearch.Client({
  host: 'http://127.0.0.1:9200',
  log: 'trace'
})

es.ping().then(response => {
  console.log(response) // true
})

What could I miss with Electron functionalities that could prevent my code to work?

Thank you all for your kind responses, and have a nice day.

EDIT: Here is the detailed error and the stack trace:

Uncaught (in promise) StatusCodeError {status: undefined, displayName: "RequestTimeout", message: "Request Timeout after 3000ms", body: false, stack: "Error: Request Timeout after 3000ms
at /home/j…_modules/elasticsearch/src/lib/transport.js:383:7"}body: falsedisplayName: "RequestTimeout"message: "Request Timeout after 3000ms"status: undefinedstack: "Error: Request Timeout after 3000ms
at /home/johndoe/Code/elastic-ui/node_modules/elasticsearch/src/lib/transport.js:354:15
at /home/johndoe/Code/elastic-ui/node_modules/elasticsearch/src/lib/transport.js:383:7"__proto__: ErrorAbstract
Hammerbot
  • 15,696
  • 9
  • 61
  • 103
  • What exactly is the promise rejected with? What is the error message? where's the stack trace? – Kevin B Aug 17 '17 at 20:09
  • I edited my question with the error message, and the stack trace. To the person who downvoted the question, could you please add some information on why you would do such a thing? I would be more than glad to edit my question in order to understand my error. – Hammerbot Aug 17 '17 at 20:21
  • i did add an explanation. – Kevin B Aug 17 '17 at 20:25
  • Oh I'm sorry I didn't know that was you, I hope the error can be of any help – Hammerbot Aug 17 '17 at 20:27

1 Answers1

3

I'm guessing that there is either a bug with elasticsearchJS and Electron or that you're doing something wrong.

Since you are using Electron, you should try to use the browser build of the package.

Instead of requiring elasticsearch, try to require elasticsearch-browser:

yarn add elasticsearch-browser // or npm install elasticsearch-browser

then just replace your

import elasticsearch from 'elasticsearch'

by

import elasticsearch from 'elasticsearch-browser'.

This version of the package will make XHRHttpRequest instead of using the native http module from node. You will then be able to monitor your request more easily with Chrome Network tab.

I hope this helps

More information here: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/browser-builds.html Chat Conversation End Type a message...

Sylvain Attoumani
  • 1,213
  • 1
  • 15
  • 34
  • Wow, that works, thanks! I suppose there is indeed something wrong. But anyways, this works great thank you! – Hammerbot Aug 18 '17 at 13:09