简体   繁体   English

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

[英]how to display data to ejs from API (express js)

Using express js, i want to render json that has been produced by my to ejs file.使用 express js,我想将我生成的 json 渲染到 ejs 文件。

here is my controller that produce json这是我的 controller 产生 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我想将 JSON 从 controller 传递到下面的路由器,然后我的路由器所做的就是将数据带入管理页面并将数据显示

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或者我的问题可能是关于如何在 js 文件之间抛出/传递数据

Don't make HTTP requests from your server back to your server.不要从您的服务器向您的服务器发出 HTTP 请求。

You have a function that gets your data.你有一个 function 来获取你的数据。 Use that function.使用那个 function。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM