【问题标题】:How can I use await in ejs file?如何在 ejs 文件中使用 await?
【发布时间】:2020-12-23 02:55:41
【问题描述】:

这不是重复的。我希望能够在我的 ejs 文件中持续执行 await,但我有一个著名的错误:SyntaxError: await is only valid in async function in C:\Users\...\dashboard.ejs while compiling ejs 您能向我解释一下它是如何工作的以及如何修复我的代码吗? :

app.js


  let ejsOptions = {
    // delimiter: '?', Adding this to tell you do NOT use this like I've seen in other docs, does not work for Express 4
    async: true
  };

  // We set out templating engine.
  app.engine("html", async (path, data, cb) => {
    try{
      let html = await ejs.renderFile(path, data, ejsOptions);
      cb(null, html);
    }catch (e){
      cb(e, '');
    }
  });


  app.get("/dashboard", checkAuth, async (req, res) => {

    await renderTemplate(res, req, "dashboard.ejs", { perms: Discord.Permissions });
  });

dashboard.ejs:



  <%
  let results;
 
    results = await bot.shard.broadcastEval(`this.guilds.cache.get('${guild.id}')`);
  
  console.log(results.id)

  if (results) {
     %>
    <a class="button is-success has-text-centered" href="/dashboard/<%= guild.id %>">Settings</a>
  <% } else { %>
    <a class="button is-primary has-text-centered" href="<%= `https://discordapp.com/oauth2/authorize?client_id=${bot.user.id}&scope=bot&guild_id=${guild.id}&response_type=code&redirect_uri=${encodeURIComponent(`${bot.config.domain}${bot.config.port == 80 ? "" : `:${bot.config.port}`}/callback`)}` %>">Invite</a>
  <% } 

    %>
   <% }); %>
<%- include("partials/footer") %>
</section>

我尝试了很多方法,但是当我尝试使用'if''else'时没有结果。

【问题讨论】:

  • EJS 文件不是函数。更重要的问题是为什么你想这样做:模板是这样的逻辑错误的地方。
  • @DaveNewton 我只需要抓住那个承诺,我有一个 forEach 方法可以给我“公会”。这会检索用户的公会,它应该检查每个公会是否存在。有没有办法在 app.js 中做到这一点?如果是,如何
  • 在获得模板数据之前不要渲染模板。
  • @KevinB 是什么意思
  • @EthanBrunet 您可能想退后一步。模板应该很“愚蠢”;几乎总是在渲染之前准备好数据。我没有看到任何循环,但处理多个异步请求在网络上有很好的记录。

标签: javascript node.js ejs


【解决方案1】:

你在像这样渲染 ejs 文件之前得到结果数据。

app.js

app.get("/dashboard", checkAuth, async (req, res) => {
    const data = await bot.shard.broadcastEval(`this.guilds.cache.get('${guild.id}')`);
    renderTemplate(res, req, "dashboard.ejs", { perms: Discord.Permissions, data });
});

dashboard.ejs

.... your code.....

  if (data != undefined) || data != null) {
     %>
    <a class="button is-success has-text-centered" href="/dashboard/<%= guild.id %>">Settings</a>
  <% } else { %>
    <a class="button is-primary has-text-centered" href="<%= `https://discordapp.com/oauth2/authorize?client_id=${bot.user.id}&scope=bot&guild_id=${guild.id}&response_type=code&redirect_uri=${encodeURIComponent(`${bot.config.domain}${bot.config.port == 80 ? "" : `:${bot.config.port}`}/callback`)}` %>">Invite</a>
  <% } 

    %>
   <% }); %>
<%- include("partials/footer") %>
</section>

根据需要进行一些更改。!!!

【讨论】:

    【解决方案2】:

    您需要在渲染模板之前准备好模板所需的所有渲染数据。对于您的 results 示例,它将类似于:

    app.get("/dashboard", checkAuth, async (req, res) => {
        const results = await bot.shard.broadcastEval(`this.guilds.cache.get('${guild.id}')`);
        renderTemplate(res, req, "dashboard.ejs", { perms: Discord.Permissions, results });
    });
    

    【讨论】:

      猜你喜欢
      • 2020-07-13
      • 2020-07-15
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 2019-04-17
      相关资源
      最近更新 更多