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