简体   繁体   中英

send data from node.js to angular with get()

I want to retrieve data (an array) from a node.js file with angular . I tried on my node.js file:

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

And I've some difficulties to retrieve it on angular. I've created a service:

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();
}

I've read that this.http.get() return an Observable<Response> but I don't know what to do with that. I've searched some documentation on it but I didn't find anything really useful.

The part pipe(map(data => this.data=data.json())) is just something that I tried but when I try to display the data object I got an undefined .

Can you help me with to understand how all of this works? Thank you!

Edit: Result of the 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

You need to subscribe to the observable

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

Try this.

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;
    });
}

You can try this solution as given :

In 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);
        }
    }

In component

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

getDictionnary(){

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

 this.data = resp.data

}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM