【问题标题】:angular 6 ERROR ReferenceError: "process is not defined" with elasticsearch jsangular 6 ERROR ReferenceError:使用elasticsearch js“未定义进程”
【发布时间】:2018-12-09 22:12:49
【问题描述】:

在一切之前,我阅读:

question 1 regarding process is not defined

question 2 regarding process is not defined

angular 5 question

browser build info

并且提供的信息都没有帮助。 我正在使用 Angular 6,我想使用 elasticsearch js 向 elasticsearch 发送查询。 我像这样安装了elasticsearch、elastic types和elastic browser:

npm install elasticsearch
npm install @types/elasticsearch
npm install elasticsearch-browser

我尝试使用--save、--save-dev 和不使用进行安装,但均无济于事。

然后我用 angular cli 创建了一个新项目:

import { Component } from '@angular/core';
import { Client } from "elasticsearch";

@Component({
  selector: 'app-root',
  template: `
<script src="/node_modules/elasticsearch/src/elasticsearch.js"></script>
  <div class="container">
  <div class="row">
      <button type="" (click)="onSendElastic()">Click Me!</button>
  </div>
</div>`,
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  elClient: Client

  onSendElastic(){
    console.log('clicked on send elstic');
    this.connect()
    let res = this.elSearch()
    console.log(`res: ${res}`);

  }

  connect(){
    console.log('start connect...');

    this.elClient = new Client({
      host:"http://elasticIp:9200",
      log:"trace"
    })
    this.elClient.ping({
      requestTimeout:2000
    },(err)=>{
      if(err){
        console.log('err');
      }
      else{
        console.log('everything is ok');

      }
    })
  }

  elSearch(){
    console.log('start search...');

    return this.elClient.search({
      index:"elastic_index",
      type:"doc",
      body:{
        "query":{
          "term":{
            "field1":"true"
          }
        }
      }
    })
  }
}

我添加了&lt;script&gt; 标签,因为我发现的一些答案建议了这一点,但它没有帮助。

的格式取自:https://www.npmjs.com/package/elasticsearch。 当我启动项目时,通过运行ng startnpm start,当我按下按钮时,我得到:

点击发送 elstic;开始连接... 错误 ReferenceError: "process 没有定义” addOutputhttp://localhost:4200/vendor.js:100911:7Loghttp://localhost:4200/vendor.js:100749:5传输http://localhost:4200/vendor.js:101610:27EsApiClienthttp://localhost:4200/ vendor.js:99098:22Clienthttp://localhost:4200/vendor.js:99141:10connecthttp://localhost:4200/main.js:104:25onSendElastichttp://localhost:4200/main.js:98:9View_AppComponent_0ng: ///AppModule/AppComponent.ngfactory.js:14:23handleEventhttp://localhost:4200/vendor.js:39410:16callWithDebugContexthttp://localhost:4200/vendor.js:40503:22debugHandleEventhttp://localhost:4200/vendor .js:40206:12dispatchEventhttp://localhost:4200/vendor.js:36869:16renderEventHandlerClosurehttp://localhost:4200/vendor.js:37313:38decoratePreventDefaulthttp://localhost:4200/vendor.js:47314:36invokeTaskhttp:// /localhost:4200/polyfills.js:2743:17onInvokeTaskhttp://localhost:4200/vendor.js:33108:24invokeTaskhttp://localhost:4200/polyfills.js:2742:17runTaskhttp://localhost:4200/polyfills.js :2510:28invokeTaskhttp://localhost:4200/polyfills.js:2818:24i nvokeTaskhttp://localhost:4200/polyfills.js:3862:9globalZoneAwareCallbackhttp://localhost:4200/polyfills.js:3888:17

我找到了使用 elasticsearch 浏览器的参考,但安装它似乎没有任何作用。 browser-bould 页面中的信息引用了 angularjs,而不是 angular 2,所以我被卡住了。

为什么会发生这个错误? 如何解决?

【问题讨论】:

  • 我也一样,你有答案吗。
  • @MPPNBD 不,抱歉。我刚刚尝试在 Angular 中使用 http 模块。

标签: javascript node.js angular elasticsearch angular6


【解决方案1】:

我们发现this thread 中的最后一个解决方案对我们有用:

具体来说:

 npm i -S process, then add this to polyfill.ts:
 import * as process from 'process';
 window['process'] = process;

【讨论】:

    【解决方案2】:

    将 Elastic 搜索客户端更新到 15.1.1 解决了类似的问题

    "elasticsearch": "^15.1.1",
    

    请试试这个。

    【讨论】:

    • 嗨,我会尝试这个(我什至认为我不会尝试新的解决方案,因为正如我所说,我转向了不同的解决方案)但没有成功。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    相关资源
    最近更新 更多