简体   繁体   中英

How do I pass parameters in Javascript + Node?

I have this function below where I want to pass a parameter into the runQuery method like runQuery(90) to select any desired row count. But because of err and connection I do not know where to place it. Any guidance is greatly appreciated.

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
    });
});

Looking at the documentation , you could try refactor your function as follows (untested):

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 
});

The prepared statement contains the parameter you wish to pass in your select statement. This of course hasn't been tested but hopefully will give you good pointers towards what you want to achieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM