簡體   English   中英

如何在不使用任何模板引擎的情況下渲染 mongoDB 數據

[英]How to render mongoDB data without using any template engine

如何在不使用任何模板引擎(ejs、車把)的情況下從 mongoDB 渲染數據??? 是否可以將數據海峽渲染到 html? 如何? 我以前從未這樣做過,也不知道該怎么做。 請幫我。!! 我很樂意提供任何信息或鏈接來閱讀它。

您可以做一件事,從 html 發出 fetch() 請求並在后端打開一個端點。

例子:-

在后端

app.get("/products", async (req, res) => {
  const products = await getProductsFromMongodb();

  res.status(200).json(products);
});

在 html

<body>
  <ul>
    <li>products lists here...</li>
  </ul>
  <script>
    async function fetchProducts() {
      try {
        const response = await fetch("/products");

        if (response.ok) {
          const products = await response.json();
          // show products name,tags ,description,etc...  in  lists of card by looping products
        } else {
          // show user friendly error inside to screen
        }
      } catch (error) {
        console.log(error);
        // show user friendly error inside to screen
      }
    }

    window.onload = () => {
      fetchProducts();
    };
  </script>
</body>

暫無
暫無

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

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