Reputation: 1
I receive the error Invalid read syntax: "]" when using console.log to print values from JavaScript array objects inside of org file code blocks. Arrays that contain strings produce this error. Arrays which have just numeric values print to the console fine.
I am not sure why org-babel is having difficulty with console.log(). I tried checking the encoding of my org file as a first step. I verified my code using node.js by itself. Specifying a different interpreter (e.g babel-cli) to evaluate the code block produces the same error.
This works
#+BEGIN_SRC js
let myarray = [1, 2, 3, 4, 5];
console.log(myarray);
#+END_SRC
#+RESULTS:
: [1 (\, 2) (\, 3) (\, 4) (\, 5)]
This does not
#+BEGIN_SRC js
let myarray = ["a", "b", "c", "d", "e"];
console.log(myarray);
#+END_SRC
Is there something I need to do within my org config files? I am using Emacs version 26.1 on Windows 7 (build 1, x86_64-w64-mingw32). Node.js is version 10.15.3 .
Upvotes: 0
Views: 855
Reputation: 7
By adding ':results output' to the header, console.log prints the content of the array.
#+begin_src js :results output
let liste = [...'Zlatan is gone'];
console.log(liste);
#+end_src
#+RESULTS:
: [
: 'Z', 'l', 'a', 't',
: 'a', 'n', ' ', 'i',
: 's', ' ', 'g', 'o',
: 'n', 'e'
: ]
Upvotes: 0
Reputation: 1
Correction:
Where I have different lines trying to return/show values inside a code block, only the first return statement produces a result (return ends the immediate scope). What seems to work is process.stdout.write('yourcodehere'+ '\n');
Example:
Trying to use multiple return statements
#+BEGIN_SRC js
return ([0,1,2,3,4]);
return ([5,6,7,8,9]);
#+END_SRC
#+RESULTS:
| 0 | 1 | 2 | 3 | 4 |
Using process.stdout.write()
#+BEGIN_SRC js
process.stdout.write([0, 1, 2, 3, 4] + '\n');
process.stdout.write(["a", "b", "c", "d", "e"] + '\n');
#+END_SRC
#+RESULTS:
: 0,1,2,3,4
: a,b,c,d,e
: undefined
Previous Message:
Ok, I found a simple solution. Use return instead of console.log().I tried the same example in Python and only got results when using return instead of print. So I tried the same with my JavaScript examples using return as the final step and this works great. The formatting of the array in the results block looks better too.
Upvotes: 0