fastify ejs mongodb don;

发布于 2025-02-10 02:41:32 字数 2427 浏览 0 评论 0原文

我正在创建一个网站,目前我正在使用MongoDB进行数据库系统,但我几乎完成了该系统,但是自从我更新了一个快速套件以来,该页面仅显示白色,而另一个带有数据的EJS页面则带有数据发送

我的软件包: 快速:4.0.3 路径:0.12.7 @fastify/view:7.0.0 MongoDB:4.7.0 Dotenv:16.0.1 EJS:3.1.8 @fastify/static:6.4.0 @fastify/formbody:7.0.1 fastify-plugin:3.0.1 Mongoose:6.4.0

我的路线代码:

fastify.get("/result", async function (request, reply) {
 let search = request.query.search;
  
  if (search) {
    await dbAnime.find({ aliases: search })
      .toArray(async function (err, have) {
      if (!err) {
          if (have.length > 0) {
             reply.view("/pages/result.ejs", { results: have }).then(() => {
                console.log("replied")
             });
         console.log(have);
          client.close;    
          } else {
              reply.view("/pages/result.ejs", { results: have });
          }
        } else {
          reply.code(400)
          console.error(err);
        }
      });
  };
});

和我的EJS页面:

<!DOCTYPE html>
<html data-theme="">
  <head>
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/card.css">
    <link rel="preconnect" href="https://fonts.googleapis.com/">
    <link rel="preconnect" href="https://cdn.glitch.global/">
    <link rel="preconnect" href="https://cdn.discordapp.com/">
    <link rel="icon" type="image/png" href=""/>
    
    <title><%= results.length %> Résultat</title>
    </head>
  <body>
 
    <div class="form-container">
              <form class="form" action="/result">
                  <input id="myInput" type="text" class="input" name="search" placeholder="Rechercher un anime..."/>
      </form>
    </div>
    <h2>Résultat: <%= results.length %></h2>
    <% results.forEach(function(result){%>
<div class="paper" id="paper"><img class="poster" src="<%= result.image %>"/>
  
  <h1><%= result.title %></h1>
  <hr/>
  <p><%= result.synopsis %></p><a class="btn" href="/info?animeName=<%= result.title %>">En savoir plus</a>
  <div class="space"></div>
</div>
      <% }); %>
  </body>

</html>

我的控制台没有任何错误,那么我不知道如何修复它

I am creating a website and I am currently making a database system with mongodb, which I have almost finished but, since I updated a fastify package, the page only displays of white, same with another ejs page with data sended

My package:
fastify: 4.0.3
path: 0.12.7
@fastify/view: 7.0.0
mongodb: 4.7.0
dotenv: 16.0.1
ejs: 3.1.8
@fastify/static: 6.4.0
@fastify/formbody: 7.0.1
fastify-plugin: 3.0.1
mongoose: 6.4.0

My route code:

fastify.get("/result", async function (request, reply) {
 let search = request.query.search;
  
  if (search) {
    await dbAnime.find({ aliases: search })
      .toArray(async function (err, have) {
      if (!err) {
          if (have.length > 0) {
             reply.view("/pages/result.ejs", { results: have }).then(() => {
                console.log("replied")
             });
         console.log(have);
          client.close;    
          } else {
              reply.view("/pages/result.ejs", { results: have });
          }
        } else {
          reply.code(400)
          console.error(err);
        }
      });
  };
});

and my ejs page:

<!DOCTYPE html>
<html data-theme="">
  <head>
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/card.css">
    <link rel="preconnect" href="https://fonts.googleapis.com/">
    <link rel="preconnect" href="https://cdn.glitch.global/">
    <link rel="preconnect" href="https://cdn.discordapp.com/">
    <link rel="icon" type="image/png" href=""/>
    
    <title><%= results.length %> Résultat</title>
    </head>
  <body>
 
    <div class="form-container">
              <form class="form" action="/result">
                  <input id="myInput" type="text" class="input" name="search" placeholder="Rechercher un anime..."/>
      </form>
    </div>
    <h2>Résultat: <%= results.length %></h2>
    <% results.forEach(function(result){%>
<div class="paper" id="paper"><img class="poster" src="<%= result.image %>"/>
  
  <h1><%= result.title %></h1>
  <hr/>
  <p><%= result.synopsis %></p><a class="btn" href="/info?animeName=<%= result.title %>">En savoir plus</a>
  <div class="space"></div>
</div>
      <% }); %>
  </body>

</html>

i don't have any error in console, then i don't know how to fix it

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

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

发布评论

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

评论(1

梦醒时光 2025-02-17 02:41:32

异步函数和回复都有混合使用。
在Fastify V4中,如果要使用send方法,则必须返回回复对象。

我将更改代码如下:

fastify.get('/result', async function (request, reply) {
  const search = request.query.search

  if (search) {
    const have = await dbAnime.find({ aliases: search }).toArray()
    if (have.length > 0) {
      return reply.view('/pages/result.ejs', { results: have })
    } else {
      return reply.view('/pages/result.ejs', { results: have })
    }
  }
})

There is a mix of async function and reply.send usages.
In Fastify v4 you must return the reply object if you want to use the send method.

I would change the code as follows:

fastify.get('/result', async function (request, reply) {
  const search = request.query.search

  if (search) {
    const have = await dbAnime.find({ aliases: search }).toArray()
    if (have.length > 0) {
      return reply.view('/pages/result.ejs', { results: have })
    } else {
      return reply.view('/pages/result.ejs', { results: have })
    }
  }
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文