【问题标题】:Why the http get is not reaching the server? [duplicate]为什么 http get 没有到达服务器? [复制]
【发布时间】:2019-12-15 05:47:18
【问题描述】:

我只想从 Mongodb 集合中获取一个文档,我在前端使用 Angular 8,在服务器上使用 Node js,所以当我请求特定的 url 来获取数据时,Angular 似乎会执行请求但是服务器没有响应。我试图在服务器端做一个控制台日志来验证是否达到了请求,但控制台日志从来没有打印出来。

这是 Angular 打字稿文件中函数的代码:

  searchUser(value) {
    if (this.query !== '' && this.query.length > 0) {
      this.ps.search(value);   //ps is the service
    } else {
      console.log('Incorrect');
    }
  }

这是服务文件中的代码:

  search(value) {

    return this
    .http
    .get(`${this.uri}/searchUser/${value}`);

  }

这是节点获取请求函数:

app.get('/searchUser/:value', function (req, res) {
    let value = req.params.value;

    console.log(value + ' <<< value in the server');

    user.find({ user: value}, function(err, doc) {
        if(err) {
          console.log(err);  
        }else {
            // res.json(res);
            console.log(doc);
        }
    });
}) 

【问题讨论】:

  • 您必须订阅来自search 的可观察回报。 this.ps.search(value).subscribe((val) =&gt;....)

标签: node.js angular mongoose


【解决方案1】:

添加subscribe

 searchUser(value) {
        if (this.query !== '' && this.query.length > 0) {
          this.ps.search(value).subscribe();   //ps is the service
        } else {
          console.log('Incorrect');
        }
      }

您只是没有触发从您的服务返回的可观察对象。

【讨论】:

  • 或者更好的是,让您的 HTML 通过将订阅绑定到 AsyncPipe 来管理订阅。确保您不会意外在应用程序中引入内存泄漏。
猜你喜欢
  • 2022-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
  • 1970-01-01
  • 2020-02-11
相关资源
最近更新 更多