axios GET请求MERN栈

Axios GET request MERN stack

我正在尝试从我的 mongoDB 中获取一些数据。 我遇到了一些有趣的错误,但我似乎无法弄清楚。 这是我对后端的前端请求。

getStockByTicker = () => { Axios.get(`http://localhost:5000/stock`, {
      ticker: this.props.ticker,
    }).then((res) => {
      const stockData = res.data;
      console.log(stockData);
    });
  };

这就是后端代码的样子,尽管 - 它似乎还没有走到那一步。

router.get("/stock", async (req, res) => {
  console.log("Hey I'm in the back end , the ticker is: " + req.symbol);
  //error
  const query = { symbol: req.symbol };
  const stock = await Stock.findOne(query);
  res.json({
    name: stock.name,
    symbol: stock.symbol,
  });
});

这是数据库的样子

_id: ObjectId("6016dfc14c9f733d7775c8ad")
symbol: "TSLA"
exchange: "NSDQ"
name: "Tesla Inc."

我遇到的错误:

xhr.js:177 GET http://localhost:5000/stock 404 (Not Found)
createError.js:16 Uncaught (in promise) Error: Request failed with status code 404
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.handleLoad (xhr.js:62)

路由器与 Express 应用之间的连接

const stocksRouter = require("./routes/stocks");
app.use("/stocks", stocksRouter);

似乎是什么问题?

app.use("/stocks", stocksRouter);
router.get("/stock"

您在路由器上的路由是 /stock,您将路由器安装在 /stocks 上,使您的最终路径 /stocks/stock 但您的客户端代码只要求 /stock.