简体   繁体   中英

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.

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

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 });
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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