简体   繁体   中英

Fastify ejs mongodb don't display the page with data

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

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

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