отправьте данные из node.js в angular с помощью get () - PullRequest
0 голосов
/ 02 июля 2018

Я хочу получить данные (массив) из файла node.js с angular. Я пробовал на моем node.js файле:

app.get('/search', function(req, res){
    res.send(data);
});

И у меня есть некоторые трудности, чтобы получить его на угловой. Я создал сервис:

data: any;

getDictionnary(): Observable<String[]> {
    var dataGet = this.http.get('/search').pipe(map(data => this.data=data.json()));
    console.dir(dataGet);
    return dataGet;
};

ngOnInit() {
    this.data = this.getDictionnary();
}

Я читал, что this.http.get() возвращает Observable<Response>, но я не знаю, что с этим делать. Я искал некоторую документацию по нему, но не нашел ничего действительно полезного.

Часть pipe(map(data => this.data=data.json())) это просто то, что я пытался, но когда я пытаюсь отобразить объект данных, я получаю undefined.

Можете ли вы помочь мне понять, как все это работает? Спасибо!

Редактировать: Результат console.get (dataGet):

Observableoperator: MapOperatorproject: ƒ (data)arguments: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]caller: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]length: 1name: ""prototype: {constructor: ƒ}constructor: ƒ (data)arguments: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]caller: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]length: 1name: ""prototype: constructor: ƒ (data)arguments: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]caller: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]length: 1name: ""prototype: {constructor: ƒ}constructor: ƒ (data)arguments: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]caller: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]length: 1name: ""prototype: {constructor: ƒ}constructor: ƒ (data)arguments: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]caller: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]length: 1name: ""prototype: {constructor: ƒ}__proto__: ƒ ()[[FunctionLocation]]: dictionnarymanager.service.ts:17[[Scopes]]: Scopes[3]__proto__: Object__proto__: ƒ ()[[FunctionLocation]]: dictionnarymanager.service.ts:17[[Scopes]]: Scopes[3]__proto__: Object__proto__: ƒ ()[[FunctionLocation]]: dictionnarymanager.service.ts:17[[Scopes]]: Scopes[3]__proto__: Object__proto__: ƒ ()[[FunctionLocation]]: dictionnarymanager.service.ts:17[[Scopes]]: Scopes[3]__proto__: Object__proto__: ƒ ()apply: ƒ apply()arguments: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]bind: ƒ bind()call: ƒ call()caller: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]constructor: ƒ Function()length: 0name: ""toString: ƒ toString()Symbol(Symbol.hasInstance): ƒ [Symbol.hasInstance]()get arguments: ƒ ()arguments: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]caller: (...)length: 0__proto__: ƒ ()[[Scopes]]: Scopes[0]set arguments: ƒ ()get caller: ƒ ()set caller: ƒ ()__proto__: Object[[FunctionLocation]]: <unknown>[[Scopes]]: Scopes[0]No properties[[FunctionLocation]]: dictionnarymanager.service.ts:17[[Scopes]]: Scopes[3]thisArg: undefined__proto__: call: ƒ (subscriber, source)arguments: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]caller: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]length: 2name: ""prototype: {constructor: ƒ}__proto__: ƒ ()[[FunctionLocation]]: map.js:17[[Scopes]]: Scopes[2]constructor: ƒ MapOperator(project, thisArg)__proto__: Objectsource: Observable_isScalar: false_subscribe: ƒ (responseObserver)arguments: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]caller: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
at Function.remoteFunction (<anonymous>:2:14)]length: 1name: ""prototype: constructor: ƒ (responseObserver)__proto__: Object__proto__: ƒ ()[[FunctionLocation]]: http.js:1016[[Scopes]]: Scopes[3]__proto__: Object_isScalar: false__proto__: Object

Ответы [ 3 ]

0 голосов
/ 02 июля 2018

Попробуйте это.

data: any;

getDictionnary(): Observable<String[]> {
    return this.http.get('/search').pipe(map(data => this.data=data.json()));
};

ngOnInit() {
    this.getDictionnary().subscribe((response) => {
       this.data = response;
    });
}
0 голосов
/ 02 июля 2018

Вы можете попробовать это решение как дано:

В HttpService:

  import { Injectable } from '@angular/core';

    import {
        HttpClient,
        HttpErrorResponse,
        HttpHeaders
    } from '@angular/common/http';

    import { Observable ,  Subject } from 'rxjs';

    @Injectable({
        providedIn: 'root'
    })
    export class HttpService {

        constructor(private httpClient: HttpClient) {


        }

      getData(url,body, options): Observable<any>{
          return this.httpClient.get(url, options);
        }
    }

В компоненте

private data: string[] = [];
constructor(private http: HttpService){
this.getDictionnary();
}

getDictionnary(){

this.http.getData(url,options).subscribe((resp:any) => {

 this.data = resp.data

}
}
0 голосов
/ 02 июля 2018

Нужно подписаться на наблюдаемые

     let observable: Observable<Response> = this.http.get('/search',requestOptions);
     observable.subscribe((response: Response) => {
     let responseData: any = response.arrayBuffer().byteLength > 0 ? response.json() : {}; 
 });
...