1

I have an ejs code to create a temp vector.

 <% var temp=[];
   for (i = 0; i < his.length; i++) { 
        temp[i]=  his[i].temp;
};%>

The variable "his" comming from the server then inside a script tag i have this.

var tempe = <%= temp; %>
console.log(tempe); 

I got the SyntaxError: missing variable name

what can I do?

Note: Mozilla debugger shows tempe as

tempe= 76,74,24,29,69,59,44`
4
  • Which line throws that error? On the server or on the browser? Commented May 14, 2015 at 5:52
  • Tempe line inside the script tag, the error is showed in the browser console, everything seens ok to me i dont know what is happening Commented May 14, 2015 at 6:50
  • Is it not for for (var i...)? Commented May 14, 2015 at 7:06
  • no, the error is inside script tag in 'Var tempe = <%= temp; %>' Commented May 14, 2015 at 15:26

1 Answer 1

1

You need to output the array as an array...

var tempe = <%= temp; %> is going to the browser as tempe = 76,74,24,29,69,59,44 as you're seeing in the debugger. What you really want is:

tempe = [76,74,24,29,69,59,44]; 

which you can get by doing this:

var tempe = <%-JSON.stringify(temp)%>;

or less neatly, this:

var tempe = [<%= temp; %>];
Sign up to request clarification or add additional context in comments.

Comments

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.