簡體   English   中英

404 未找到(API url 未找到,即使它 100% 存在)

[英]404 not found (API url not found even though it 100% exists)

我正在嘗試通過 id 獲取產品。 現在,當我用 postman 嘗試這個時,它工作得非常好,沒有任何問題。 但是,當我嘗試使用 Angular 獲取數據時,它沒有工作,它一直說 404 not found 我不知道是什么問題。 請有知道的人告訴我。 任何幫助將不勝感激。 這是我的代碼。

快遞.js:

路線:

router.get("/get-product/:id", async (req, res) => {
  await fetchProductById(req, res);
});

實用程序:

const fetchProductById = async (req, res) => {
  const id = req.params.id;
  const prod = await Product.findOne({_id: id});
  if (prod) {
    if (prod.length == 0)
      res.status(200).json({
        message: "No Products",
        success: true,
      });
    else {
      res.status(200).json({
        prod,
        success: true,
      });
    }
  } else {
    res.status(400).json({
      prod,
      message: "couldn't find any product",
      id: req.id,
      success: false,
    });
  }
};

Angular:

現在 angular 服務:

getProductById(id: any){
        return this.http.get<Products[]>(
            environment.api + "products/get-product?id="+id
        )
    }

訂閱組件內的服務:

    let id = this.route.snapshot.params.id;
    this._product.getProductById(id).subscribe(
      (data: Products[])=>{
        console.log(data)
      },
      error =>  {
        console.log("there has been an error trying to fetch this product: "+id)
      }
    )

您使用了查詢參數而不是 url 參數。 它應該是"products/get-product/"+id

getProductById(id: any){
        return this.http.get<Products[]>(
            environment.api + "products/get-product/"+id
        )
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM