Skip to content Skip to sidebar Skip to footer

Passing Variable From Nodejs To Ejs File

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(). while running my code i have 'nbrow not defined'

Solution 1:

You are using single = instead of ==

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.

Solution 2:

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

Try to put const or let in front of it :

const nbrow = result.length;

Solution 3:

<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

Post a Comment for "Passing Variable From Nodejs To Ejs File"