当使用 console.log 打印 org 文件代码块内 JavaScript 数组对象的值时,我收到错误 无效读取语法:“]”。包含字符串的数组会产生此错误。仅具有数值的数组可以正常打印到控制台。
我不确定为什么 org-babel 在使用 console.log() 时遇到困难。作为第一步,我尝试检查我的组织文件的编码。我使用node.js 本身验证了我的代码。指定不同的解释器(例如 babel-cli)来评估代码块会产生相同的错误。
这有效
#+BEGIN_SRC js
let myarray = [1, 2, 3, 4, 5];
console.log(myarray);
#+END_SRC
#+RESULTS:
: [1 (\, 2) (\, 3) (\, 4) (\, 5)]
这不是
#+BEGIN_SRC js
let myarray = ["a", "b", "c", "d", "e"];
console.log(myarray);
#+END_SRC
我的组织配置文件中需要做些什么吗?我在 Windows 7 上使用 Emacs 版本 26.1(版本 1,x86_64-w64-mingw32)。 Node.js 版本为 10.15.3 .
更正:
当我有不同的行尝试在代码块内返回/显示值时,只有第一个 return 语句会产生结果(return 结束直接作用域)。似乎有效的是 process.stdout.write('yourcodehere'+ ' ');
示例:
尝试使用多个返回语句
#+BEGIN_SRC js
return ([0,1,2,3,4]);
return ([5,6,7,8,9]);
#+END_SRC
#+RESULTS:
| 0 | 1 | 2 | 3 | 4 |
使用 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
上一条消息:
好吧,我找到了一个简单的解决方案。使用 return 代替 console.log()。我在Python中尝试了相同的示例,只有在使用return而不是print时才得到结果。所以我尝试了同样的方法 我的 JavaScript 示例使用 return 作为最后一步,这有效 伟大的。结果块中数组的格式看起来更好 也是。
这有效:
#+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'
: ]