0

I have a user object having last_level property I am passing it in my express route as follows :

app.get('/game', (req, res) => {
    res.render('main.ejs', { user: req.user })
})

I want to access it in my JS script file.

After some research, I tried this...

var x = <%- user.last_level %>  

But its not working (not giving me value of last_level to assign it to variale x). Help me out on this please.

This is my whole function -

window.onload = function () {
    var x = <%- user.last_level %> 
    if (x === "Easy") {
        console.log("easy")
    } else if (x === "Hard") {
        console.log("hard")
    } else if (x === "Pro") {
        console.log("pro")
    }
    console.log(x)
};
7
  • You should try var x = user.last_level without tags. Commented May 8, 2020 at 10:26
  • is your user object loaded before your script file? var x = user.last_level though why would you need to set it to a variable if its there already? Commented May 8, 2020 at 10:26
  • Just a heads up for the future, But its not working is about the worst way to "describe" a problem. Commented May 8, 2020 at 10:28
  • Its giving user is not defined at window.onload error @federico-moretti Commented May 8, 2020 at 10:48
  • I am loading script file at the bottom of my page. The user object is passed by express. @cracierjack Commented May 8, 2020 at 10:50

1 Answer 1

1

Stringify your variable and see the results. Use the below code in your ejs template

 <script>
    window.onload = function () {
            var x =  <%- JSON.stringify(user.last_level) %>
            if (x === "Easy") {
                console.log("easy")
            } else if (x === "Hard") {
                console.log("hard")
            } else if (x === "Pro") {
                console.log("pro")
            }
            console.log(x)
        };
    </script>

Alternative solution is

 <script>
    window.onload = function () {
      <% if (user.last_level == 'Easy') { %>
        console.log("easy")
      <% } else if((user.last_level == 'Hard')  { %>
        console.log("hard")
      <% } else if(user.last_level == 'Pro')  { %>
        console.log("pro")
      <% } %>
    };
  </script>
Sign up to request clarification or add additional context in comments.

4 Comments

Can you please specify the error, since I have tried and tested it. It is working. Which solution you have tried.@IROC
for the first one Uncaught SyntaxError: Unexpected token '<' & for second one, I put it in the webpage itself at bottom, it is showing all red code and not matching to any correct syntax format.
Here is my route call code app.get('/game', (req, res) => { res.render('main.ejs', { user: req.user }) })
You should use the first solution in script tag. Like I have edited above. Since we are using JSON.stringify function. And you might be observing red code since you are not having ejs support in your editor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.