我想编写一个与 nodejs 一起使用并在 sqlite3 数据库上运行的 JS 脚本。计划是从数据库中获取数据,并使用“脚本”标签。我的片段如下:
var sqlite3 = require('sqlite3');
var db = new sqlite3.Database('/var/www/db/js.sqlite');
var MyRandomCode =
{
code: "<script>"
}
function appendToCode(snippet) {
MyRandomCode.code += snippet;
}
function printMe() {
console.log(MyRandomCode.code);
}
function closeCode() {
MyRandomCode.code += "</script>";
}
printMe();
randomNode("defun", MyRandomCode);
printMe();
closeCode();
function randomNode(name, MyRandomCode) {
db.get("SELECT tag_value FROM tags WHERE tag_name=\"" + name + "\"", function(err, rows) {
if (err) throw err;
appendToCode(rows['tag_value']);
});
}
数据库只包含一个“测试”功能实现。预期结果是:
<script>
function test() {
a = 5;
c += a + b;
"foo";
var foo = 5;
const bar = 6, baz = 7;
switch ("foo") {
case "foo":
return 1;
case "bar":
return 2;
}
for (var i = 0; i < 5; ++i) {
console.log("Hello " + i);
}
for (var i in [ 1, 2, 3 ]) {
console.log(i);
}
for (var i = 0; i < 5; ++i) console.log("foo");
}
</script>
尽管如此,我得到了
<script></script>function test() {
a = 5;
c += a + b;
"foo";
var foo = 5;
const bar = 6, baz = 7;
switch ("foo") {
case "foo":
return 1;
case "bar":
return 2;
}
for (var i = 0; i < 5; ++i) {
console.log("Hello " + i);
}
for (var i in [ 1, 2, 3 ]) {
console.log(i);
}
for (var i = 0; i < 5; ++i) console.log("foo");
}
我不明白为什么字符串的顺序会改变。为什么“脚本”标签内没有函数字符串?