【问题标题】:HTTP GET param not passing to Express app.get [duplicate]HTTP GET 参数未传递给 Express app.get [重复]
【发布时间】:2020-02-21 04:29:36
【问题描述】:

当我发出 GET 请求时,我 console.log(response) 得到一个 404

  Http failure response for http://localhost:3000/api/watchings/?watchItemUniqueKey=5dab6083b68bd049c0b8f9ce%7C5daf5e0c8d261e5364acd8b6: 404 Not Found", err

所以我知道该值是作为参数传递的,但我的 app.js 文件没有看到它。此外,app.get 开头的 console.log("output: " + req.params.test); 没有输出,因此来自 watch-list.service.ts 的 GET 请求没有命中 app.js 文件。 任何帮助表示赞赏。我一个月前才开始学习 nodejs,所以还在研究它是如何工作的。

watch-list.service.ts

 getWatchList(watchingId) {

    watchingId = watchingId.trim();
    let params1 = new HttpParams().set('watchItemUniqueKey', watchingId);

    return this.http.get<{}>(
        'http://localhost:3000/api/watchings/', {params: params1}
      ).
    subscribe((response) => {
        console.log(response);

      });
  }

app.js

app.get("/api/watchings/:test", (req, res, next) => {
console.log("output: " + req.params.test);
  Watching.find({"value": req.params.test})
  .then(documents =>{
  res.status(200).json({
    message: "Posts fetched Successfully",
    watchItems: documents,
  });

});
});

【问题讨论】:

    标签: node.js angular express mongoose


    【解决方案1】:

    您传递的是查询参数,您应该可以按如下方式访问它:

    app.get("/api/watchings", (req, res, next) => {
    console.log("output: " + req.query.watchItemUniqueKey);
      Watching.find({"value": req.query.watchItemUniqueKey})
      .then(documents =>{
      res.status(200).json({
        message: "Posts fetched Successfully",
        watchItems: documents,
      });
    
    });
    });
    

    【讨论】:

    • 非常感谢!下次我一定会记得的。
    猜你喜欢
    • 2014-04-23
    • 2013-06-13
    • 2019-02-02
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 2019-06-03
    • 1970-01-01
    相关资源
    最近更新 更多