简体   繁体   English

带有Electron的ElasticSearchJS

[英]ElasticSearchJS with Electron

I'm using elasticsearchjs in a small project using electron. 我在一个使用电子的小项目中使用elasticsearchjs。 But I somehow encounter something strange that blocks me. 但是我不知何故遇到了使我受阻的奇怪事物。

In Electron, I have a button, that triggers a function on a click: 在Electron中,我有一个按钮,单击可触发功能:

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

And the following Javascript: 以及以下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... 我可以看到hello world输出,但是弹性搜索功能不起作用,我收到超时错误...

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: 但是,如果我将该函数的调用加倍,并向elasticsearch API添加一个异步调用,则它可以工作,并且我进入了两个then()调用:

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. 而且,如果我只放置setTimeout()函数,那么它也不起作用,就像我需要两次调用该函数才能使其正常工作一样。

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. 我猜是ElasticsearchJS和Electron存在错误,或者您做错了什么。

Since you are using Electron, you should try to use the browser build of the package. 由于使用的是Electron,因此应尝试使用该软件包的浏览器版本。

Instead of requiring elasticsearch, try to require elasticsearch-browser: 而不是需要elasticsearch,请尝试使用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. 该版本的软件包将生成XHRHttpRequest,而不是使用node的本地http模块。 You will then be able to monitor your request more easily with Chrome Network tab. 然后,您可以使用Chrome网络标签更轻松地监控您的请求。

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... 此处的更多信息: https : //www.elastic.co/guide/zh-CN/elasticsearch/client/javascript-api/current/browser-builds.html聊天会话结束键入消息...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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