简体   繁体   中英

ElasticSearchJS with Electron

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

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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