javascript - angular 6 ERROR ReferenceError:使用elasticsearch js“未定义进程”
问题描述
在一切之前,我读到:
并且提供的信息都没有帮助。我正在使用 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 start
or 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,所以我被卡住了。
为什么会发生此错误?如何解决?
解决方案
我们发现这个线程中的最后一个解决方案对我们有用:
具体来说:
npm i -S process, then add this to polyfill.ts:
import * as process from 'process';
window['process'] = process;
推荐阅读
- php - Symfony Panther - 如何在本地主机上保持 SSL?
- python - 无法从 Python 中的 SOAP 请求生成响应
- javascript - 如何在节点 js 中将用户重定向到他们的特定页面
- python - 使用 Telethon 时运行循环
- c# - 为什么 Quartz .NET 3.0.7 调度程序在 DB 不可用错误上挂起
- jquery - Bootbox将微调器图标添加到按钮onclick?
- c - 为什么在这种情况下我不能使用分配的指针?
- python - 图像亮度量化
- optaplanner - 过滤器阻止分配事实
- android-studio - android studio HAXM 安装问题