首页 > 解决方案 > angular 6 ERROR ReferenceError:使用elasticsearch js“未定义进程”

问题描述

在一切之前,我读到:

关于流程的问题1未定义

关于流程的问题 2 未定义

角度5问题

浏览器构建信息

并且提供的信息都没有帮助。我正在使用 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"
          }
        }
      }
    })
  }
}

我添加了<script>标签,因为我发现的一些答案暗示了这一点,但它没有帮助。

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

点击发送 eltic;开始连接... ERROR ReferenceError: "process is not defined" addOutputhttp://localhost:4200/vendor.js:100911:7Loghttp://localhost:4200/vendor.js:100749:5Transporthttp://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:

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

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

标签: javascriptnode.jsangularelasticsearchangular6

解决方案


我们发现这个线程中的最后一个解决方案对我们有用:

具体来说:

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

推荐阅读