找不到./node_modules/@elastic/elasticsearch/lib/Connection.js模块中的错误:错误:无法解析“ http”

时间:2019-05-30 18:06:04

标签: angular typescript elasticsearch angular6

我正在处理一个Angular 6项目,并且在服务器上将elasticsearch的版本从6更新为7后,将旧API的代码更改为使用新API后,发生了这些错误,请问有人知道如何纠正?

angular应用程序是在Visual Studio代码上创建的,而elasticsearch是在linux服务器上。

import { Injectable } from '@angular/core';
// import { Clientex } from 'elasticsearch-browser';
import { Client } from '@elastic/elasticsearch';
// import * as elasticsearch from 'elasticsearch-browser';
// import { concatMapTo } from 'rxjs/operators';
// import { stringify } from '@angular/core/src/render3/util';


@Injectable({
  providedIn: 'root'
})

export class ElasticsearchService {

  private client: Client;
  // private clientex: Clientex;

  constructor() {
    if (!this.client) {
      this.connect();
    }
  }

  private connect() {
    /*this.client = new Client(
      {node: 'http://localhost:9200'}
    );*/
    this.client = new Client();
  }

  isAvailable(): Promise<any> {
    return this.client.ping({
    });
  }


  // tslint:disable-next-line:member-ordering
  private queryalldocs = {
    'query': {
      'match_all': {}
    }
  };

  getAllDocuments(_index, _type): any {
    this.client.search({
      index: _index,
      body: this.queryalldocs
    }, {
      ignore: [404]
    }, (err, { body, statusCode, headers, warnings }) => {
      if (err) { console.log(err); }
    });
}

  getAllDocumentsByKey(_index, _type, campo, valor) {
    const string = 'let match = {' + campo + ':' + valor + '}';
    return this.client.search({
      index: _index,
      type: _type,
      body: {
           // tslint:disable-next-line:no-shadowed-variable
           eval(string) {}
      }
    });
  }

  createDocument(value: any) {
    // return this.client.index(value);
    this.client.create(value);
  }

}

错误:

  ./node_modules/@elastic/elasticsearch/lib/Connection.js中的

错误   找不到模块:错误:无法解析“ http”   'C:\ Users \ ICARO \ Desktop \ projetos \ pastas \ xtr-social \ node_modules @ elastic \ elasticsearch \ lib'

     ./node_modules/@elastic/elasticsearch/lib/Connection.js中的

错误   找不到模块:错误:无法解析“ https”   'C:\ Users \ ICARO \ Desktop \ projetos \ pastas \ xtr-social \ node_modules @ elastic \ elasticsearch \ lib'

     ./node_modules/@elastic/elasticsearch/lib/Transport.js模块中的

错误   找不到:错误:无法解析“ os”   'C:\ Users \ ICARO \ Desktop \ projetos \ pastas \ xtr-social \ node_modules @ elastic \ elasticsearch \ lib'

     ./node_modules/decompress-response/index.js中的

ERROR找不到模块:   错误:无法解析“流”中的   'C:\ Users \ ICARO \ Desktop \ projetos \ pastas \ xtr-social \ node_modules \ decompress-response'

     ./node_modules/@elastic/elasticsearch/lib/Transport.js模块中的

错误   找不到:错误:无法解析“ zlib”   'C:\ Users \ ICARO \ Desktop \ projetos \ pastas \ xtr-social \ node_modules @ elastic \ elasticsearch \ lib'

     ./node_modules/decompress-response/index.js中的

ERROR找不到模块:   错误:无法解析“ zlib”   'C:\ Users \ ICARO \ Desktop \ projetos \ pastas \ xtr-social \ node_modules \ decompress-response'

2 个答案:

答案 0 :(得分:0)

我认为您需要先安装elasticsearch-browser,然后:

import { Client } from 'elasticsearch-browser';

答案 1 :(得分:0)

根据弹性搜索文档(https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/5.x/breaking-changes.html),所有浏览器客户端均通过elasticsearch-browser npm软件包进行处理:

不再支持浏览器,因为它将通过另一个模块@ elastic / elasticsearch-browser分发。该模块仅适用于Node.js。