简体   繁体   中英

How to convert a ruby array output into a string in JS?

I have this line coming from a helper in the BE and I want to set a variable with that output. No matter what I tried (toString, string interpolation etc) It keeps returning me the error:

Uncaught ReferenceError: a is not defined at eval (eval...

This is my code:

document.myStr = <%= get_str %>

This is the result:

document.myStr = a,b,c;

All JavaScript arrays need to be enclosed with two square brackets, [ and ] .

Plus, it looks like you are writing in a , and the interpreter sees the token a (which is assumed to be a variable's name) and it looks for a variable named a .

Since you do not seem to have declared a already, it throws an error as a is now undefined .

What you can do is declare a earlier or enclose it within quotes, like 'a' or "a" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM