将变量从Node.js传递到EJS文件 [英] Passing variable from nodejs to ejs file

查看:300
本文介绍了将变量从Node.js传递到EJS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要从NodeJ传递到EJS文件的变量. 我需要此变量来选择是否显示alert().

I have a variable that i want to pass from NodeJs to EJS file . I need this variable to choose whether or not I display an alert().

在运行我的代码时,我有未定义nbrow"

while running my code i have "nbrow not defined"

server.js

server.js

con.query("SELECT * FROM entreprises WHERE identifiant =?",user, function (err, result) {
if (err) throw err;
console.log(result);
nbrow = result.length;
if(nbrow > 0)
{
    res.send("Connexion info :" + user + " " + password);    
}
else
{
    res.render("connexion",{ nbrow: nbrow});

}
});

connexion.ejs

connexion.ejs

   <script>
        if(<%= nbrow %> = 0){
            alert("user not exist");
        }
    </script>

我已经在互联网上搜索了我的代码可能有什么问题,但没有发现任何可以帮助我的东西.

I already searched on internet what could be wrong with my code but i found nothing that could help me.

我的如果"对于我的查询来说工作正常,并且我已连接到数据库.

my "if" is working fine same for my query and i'm connected to my database .

重新有人问我视图引擎的配置,我真的不知道那是什么,但是我想这是:

RE EDIT : someone asked me the view engine configuration, i don't really know what's that but i guess this is :

app.set('views', './views');
app.set('view engine', 'ejs');

推荐答案

您正在使用单个=而不是==

You are using single = instead of ==

应该是:

<script>
    if(<%= nbrow %> == 0){
        alert("user not exist");
    }
</script>

也-不管出现此特定错误.

Also - regardless to this specific error.

EJS呈现为html文件,因此在您的浏览器中,您始终可以使用查看源代码"并查看实际呈现的HTML.这样,您就可以调试问题是否出在打印nbrow或脚本中.

EJS is rendered to html file, so in your browser you can always use 'view source' and see the actual rendered HTML. This way you can debug if the problem is in printing nbrow or the script.

这篇关于将变量从Node.js传递到EJS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆