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().
我有一个变量,我想从NodeJs传递到EJS文件。我需要这个变量来选择是否显示警告()。
while running my code i have "nbrow not defined"
在运行我的代码时我有“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 .
我的“if”对我的查询工作正常,我已连接到我的数据库。
RE EDIT : someone asked me the view engine configuration, i don't really know what's that but i guess this is :
RE EDIT:有人问我视图引擎配置,我真的不知道那是什么,但我想这是:
app.set('views', './views');
app.set('view engine', 'ejs');
3 个解决方案
#1
0
You are using single =
instead of ==
您正在使用single =而不是==
It should be:
它应该是:
<script>
if(<%= nbrow %> == 0){
alert("user not exist");
}
</script>
Also - regardless to this specific error.
另外 - 不管这个特定的错误。
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.
EJS呈现为html文件,因此在浏览器中,您始终可以使用“查看源”并查看实际呈现的HTML。这样,如果问题出在打印nbrow或脚本中,您可以调试。
#2
0
My guess would be that the problem comes from this line :
我的猜测是问题来自这条线:
nbrow = result.length;
This assumes that nbrow has already been defined elsewhere, but that's not the case, hence the "nbrow is not defined"
error
这假设nbrow已经在其他地方定义过,但情况并非如此,因此“nbrow未定义”错误
Try to put const
or let
in front of it :
尝试将const或let放在它前面:
const nbrow = result.length;
#3
0
<script>
var nbrow = JSON.parse('<%- JSON.stringify(nbrow) %>');
if(nbrow == 0)
alert("user not exist");
</script>
I do by stringify the variable and parse it back
我通过stringify变量并解析它
#1
0
You are using single =
instead of ==
您正在使用single =而不是==
It should be:
它应该是:
<script>
if(<%= nbrow %> == 0){
alert("user not exist");
}
</script>
Also - regardless to this specific error.
另外 - 不管这个特定的错误。
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.
EJS呈现为html文件,因此在浏览器中,您始终可以使用“查看源”并查看实际呈现的HTML。这样,如果问题出在打印nbrow或脚本中,您可以调试。
#2
0
My guess would be that the problem comes from this line :
我的猜测是问题来自这条线:
nbrow = result.length;
This assumes that nbrow has already been defined elsewhere, but that's not the case, hence the "nbrow is not defined"
error
这假设nbrow已经在其他地方定义过,但情况并非如此,因此“nbrow未定义”错误
Try to put const
or let
in front of it :
尝试将const或let放在它前面:
const nbrow = result.length;
#3
0
<script>
var nbrow = JSON.parse('<%- JSON.stringify(nbrow) %>');
if(nbrow == 0)
alert("user not exist");
</script>
I do by stringify the variable and parse it back
我通过stringify变量并解析它