【问题标题】:send data from node.js to angular with get()使用 get() 从 node.js 发送数据到 Angular
【发布时间】:2025-12-12 00:55:02
【问题描述】:

我想用angularnode.js 文件中检索数据(一个数组)。 我尝试了我的node.js 文件:

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

而且我在 Angular 上检索它有一些困难。 我创建了一个服务:

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&lt;Response&gt; 但我不知道该怎么办。我搜索了一些关于它的文档,但没有发现任何真正有用的东西。

pipe(map(data =&gt; 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

【问题讨论】:

  • 我假设(来自res.send)您正在使用 ExpressJS?如果是这样,请标记它。此外,您的 Angular 代码运行的页面是否与 /search 位于同一 origin 中? (相对路径的使用表明“是”,但是...)
  • 我在你的情况下使用过你没有在角度获取方法中使用正确的路径
  • 我想在页面http://localhost:4200/search 上拥有数据,所以我必须在get 中输入'/search' 就这样吗?
  • app.get('/search', function(req, res){ console.log(data) res.send(data); }); 喜欢这个。

标签: javascript node.js angular express get


【解决方案1】:

你需要订阅 observable

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

【讨论】:

    【解决方案2】:

    试试这个。

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

    【讨论】:

    • 我试过了,但还是一样。我在我的服务中创建了一个简单的Display() 方法,它只包含一个console.log(data),但是当我在我的组件中运行这个方法时,我只有一个undefined
    • 您可以在getDictionnary 中添加一个控制台来获取数据的输出吗?请更新答案
    • 使用 console.log() 我只知道我的对象是 Observable
    • 能否添加后端发送的json响应
    • 我没有 JSON,但我添加了我在控制台中得到的内容
    【解决方案3】:

    你可以试试这个解决方案:

    在 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() 中写选项?
    • 这里的 options 是请求的额外参数: options = { headers: new HttpHeaders({ "Authorization": "Bearer" + this.session.get("access_token"), "Content-Type" : "应用程序/json" }) }