如何从API(express js)将数据显示到ejs

发布于 2025-01-20 14:33:00 字数 449 浏览 1 评论 0原文

使用Express JS,我想渲染由我的EJS文件产生的JSON。

这是我的控制器生产JSON,

const getAllQuotes = asyncWrapper(async (req, res) => {
  const quotes = await qSchema.find({});
  res.status(200).json({ quotes });
});

我想将JSON从控制器传递到下面的路由器,然后我的路由器的作用是带上数据并将数据显示到管理页面

adminRoute.get('/', async (req, res) => {
  //what should i type here?
        res.render("admin")
})

,或者我的问题是关于如何抛出/传递数据的问题在JS文件之间

Using express js, i want to render json that has been produced by my to ejs file.

here is my controller that produce json

const getAllQuotes = asyncWrapper(async (req, res) => {
  const quotes = await qSchema.find({});
  res.status(200).json({ quotes });
});

I want to pass the JSON from controller to my router below, then what my router does is bring the data and show the data to admin page

adminRoute.get('/', async (req, res) => {
  //what should i type here?
        res.render("admin")
})

or maybe my question is about how the data can be thrown/passed in between js file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的痛♀有谁懂 2025-01-27 14:33:00

不要从您的服务器向您的服务器发出 HTTP 请求。

您有一个获取数据的函数。使用该功能。

adminRoute.get('/', async (req, res) => {
    const quotes = await qSchema.find({});
    res.render("admin", { quotes });
})

Don't make HTTP requests from your server back to your server.

You have a function that gets your data. Use that function.

adminRoute.get('/', async (req, res) => {
    const quotes = await qSchema.find({});
    res.render("admin", { quotes });
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文