1

我在从 Node.JS 中的 sqlite (sqlite3) 库写入套接字(使用 http)时遇到问题

https://gist.github.com/RyanCopley/6004c3ce372e060bbf18

第 68 到 75 行,我有 4 次尝试写。在 db.each 之外,一切都可以在任何情况下工作。在其中,它惨遭崩溃。我不完全确定为什么,但我觉得这两个库之间存在冲突

顺便说一句,我已经知道连接 SQL 语句是不好的:3

4

1 回答 1

1

这是因为里面的回调函数db.each异步调用的。这意味着第 79 行:res.end()将在之前调用res.write("Found row!");,触发错误。

我认为你想要做的是这样的:

db.serialize(function() {
    that.res.write("["); // works
    db.each("SELECT * FROM messages WHERE channel = '"+q.chan+"' AND id > "+q.since+" ORDER BY id", function(err, row) {
        res.write("Found row!"); //does not work
        that.res.write("Found row!"); //does not work
        console.log("Found row!");
    });

    res.write("]");//works
});
于 2013-02-20T04:53:23.673 回答