使用get()将来自node.js的数据发送到angular [英] send data from node.js to angular with get()

查看:47
本文介绍了使用get()将来自node.js的数据发送到angular的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 angular node.js 文件中检索数据(数组).我试过了 node.js 文件:

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

我已经读到 this.http.get()返回一个 Observable< Response> ,但是我不知道该怎么做.我已经搜索了一些文档,但是没有发现任何真正有用的东西.

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.

部分 pipe(map(data =&>; this.data = data.json()))只是我尝试过的,但是当我尝试显示数据对象时,我得到了未定义.

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!

console.get(dataGet)的结果:

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 can try this solution as given :

在HttpService中:

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

在组件中

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

getDictionnary(){

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

 this.data = resp.data

}
}

这篇关于使用get()将来自node.js的数据发送到angular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆