如何通过角度组件在快速api端点中调用搜索功能?

时间:2019-08-23 04:33:40

标签: node.js angular typescript express

我目前拥有一个带有各种端点的快速API,用于对mongodb中的“思想”文档集合执行不同的功能。 api具有带有端点

的搜索功能
/ideas/search 

这条路线使用后请求功能处理,如下所示

exports.idea_search = function(req, res){
  console.log('search params', req.body);
if(req.body.searchTerm){
  Idea.find({name: req.body.searchTerm}).then(doc => res.json(doc));
  }
};

此控制器功能的路线如下

router.post('/search', ideas_controller.idea_search);

,当我使用静态html作为角度的临时占位符时,我得到了正确的文档,这些文档的name属性值与输入中输入的req.query.searchTerm参数匹配,并与以json格式返回的请求一起发送时没有问题。

但是当我使用以下函数从角度向该端点发出发布请求时:

public searchIdeas(search){
    return this.httpClient.post(`${this.apiURL}/search`, search);
  }

它等待很长时间,然后我将这些错误记录到控制台

`POST http://localhost:3000/ideas/search net::ERR_EMPTY_RESPONSE

HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "http://localhost:3000/ideas/search", ok: false, …}
error: ProgressEvent {isTrusted: true, lengthComputable: false, loaded: 0, total: 0, type: "error", …}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)}
message: "Http failure response for http://localhost:3000/ideas/search: 0 Unknown Error"
name: "HttpErrorResponse"
ok: false
status: 0
statusText: "Unknown Error"
url: "http://localhost:3000/ideas/search"`

这是在component.ts文件中利用上面的searchIdeas()函数的函数

searchIdeas(search){
  return this.apiService.searchIdeas(search).subscribe(
    data => this.idea = data);
  };

,我一无所获,请求保持待处理状态。

我知道这与angular调用此函数有关,因为它可以在静态html api模板中正常工作。

非常感谢您的帮助

0 个答案:

没有答案