如何在Javascript + Node中传递参数?

时间:2014-03-19 10:33:47

标签: javascript node.js

我在下面有这个函数,我想将参数传递给runQuery方法,如runQuery(90),以选择任何所需的行数。但由于错误和连接,我不知道在哪里放置它。非常感谢任何指导。

var runQuery = oracle.connect(connectData, function(err, connection, par1) {
    if (err) {
        console.log("Error connecting to db:", err);
        return;
    }

    connection.execute("SELECT * FROM HR where rownum <" + par1, [], function(err, results) {
        if (err) {
            console.log("Error executing query:", err);
            return;
        }

        console.log(results);
        connection.close(); // call only when query is finished executing
    });
});

1 个答案:

答案 0 :(得分:0)

查看documentation,您可以尝试重构您的函数,如下所示(未经测试):

function runQuery(stmt, row_num, cb) {
    stmt.execute([row_num], function(err, count) {
        if (err) return cb(err);
        if (count !== 1) return cb(new Error("bad count: " + count));
        // We are done
        return cb();
    });
}

var statement = connection.prepare("SELECT * FROM HR where rownum < :1");
runQuery(statement, 90, function(err) {
    if (err) {
        console.log("Error executing query:", err);
        return;
    }

    console.log(results);
    connection.close(); // call only when query is finished executing 
});

预准备语句包含您希望在select语句中传递的参数。这当然没有经过测试,但希望能为你提供你想达到的目标的好指点。