Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何为angular应用程序获取适当的API数据?_Node.js_Angular_Rest_Api_Express - Fatal编程技术网

Node.js 如何为angular应用程序获取适当的API数据?

Node.js 如何为angular应用程序获取适当的API数据?,node.js,angular,rest,api,express,Node.js,Angular,Rest,Api,Express,我用nodejs、express和mongodb创建了一个API服务。当我用postman甚至在浏览器上测试它时,它工作正常。然而,当我尝试从angular应用程序中执行相同操作时,它给出了一个奇怪的响应 这是我从邮递员那里发出的get请求: api服务的角度代码: url = 'http://localhost:3000'; getList() { return this.http.get(this.url); } 当我在app.component.ts中执行以下操作时 console

我用nodejs、express和mongodb创建了一个API服务。当我用postman甚至在浏览器上测试它时,它工作正常。然而,当我尝试从angular应用程序中执行相同操作时,它给出了一个奇怪的响应

这是我从邮递员那里发出的get请求:

api服务的角度代码:

url = 'http://localhost:3000';
getList() {
  return this.http.get(this.url);
}
当我在app.component.ts中执行以下操作时

console.log(JSON.stringify(this.taskapi.getList()));
这是我在控制台中看到的:

{"_isScalar":false,"source":{"_isScalar":false,"source":{"_isScalar":false,"source":{"_isScalar":true,"value":{"url":"http://localhost:3000","body":null,"reportProgress":false,"withCredentials":false,"responseType":"json","method":"GET","headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"params":{"updates":null,"cloneFrom":null,"encoder":{},"map":null},"urlWithParams":"http://localhost:3000"}},"operator":{"concurrent":1}},"operator":{}},"operator":{}}

我希望得到和邮递员一样的回答。我怎样才能做到这一点呢?

您应该订阅observable以获得结果。可观察对象是懒惰的,除非您订阅,否则它不会启动调用

getList(){
返回this.http.get(this.url);
}
清单:任何;
this.taskapi.getList().subscribe((响应)=>{
this.list=响应;
//在这里你会得到回应
});
您还可以使用angularasync管道,它将自动为您订阅和取消订阅


您应该订阅observable以获得结果。可观察对象是懒惰的,除非您订阅,否则它不会启动调用

getList(){
返回this.http.get(this.url);
}
清单:任何;
this.taskapi.getList().subscribe((响应)=>{
this.list=响应;
//在这里你会得到回应
});
您还可以使用angularasync管道,它将自动为您订阅和取消订阅


您的getList()函数正在返回一个
可观察的
,这就是
this.http.get
返回的内容

只需订阅Observable并打印响应,如下所示:

this.taskapi.getList().subscribe(
  res =>
  {
    console.log(res);
  },
  err =>
  {
    /*Error handling*/
  }
)
您的getList()函数正在返回一个
可观察的
,这就是
this.http.get
返回的内容

只需订阅Observable并打印响应,如下所示:

this.taskapi.getList().subscribe(
  res =>
  {
    console.log(res);
  },
  err =>
  {
    /*Error handling*/
  }
)

@很高兴来到这里!。将此另存为数组意味着,您是否要再次向服务器投递?你能解释一下你想做什么吗?@San如果你想在html中作为数组进行迭代,请查看更新的答案。您可以将响应数组保存在另一个属性集合中,并且可以在html中使用,而无需async pipe.nvm,我已经让它工作了。我只是想把响应保存在一个局部变量中,以便以后可以访问。结果证明这是一个类型问题。无论如何,谢谢!:)@很高兴来到这里!。将此另存为数组意味着,您是否要再次向服务器投递?你能解释一下你想做什么吗?@San如果你想在html中作为数组进行迭代,请查看更新的答案。您可以将响应数组保存在另一个属性集合中,并且可以在html中使用,而无需async pipe.nvm,我已经让它工作了。我只是想把响应保存在一个局部变量中,以便以后可以访问。结果证明这是一个类型问题。无论如何,谢谢!:)