我得到“客户未定义”。我可以看到没有为该函数定义客户端,但我不确定如何将客户端传递给异步。我不认为我实际上将每个正确的值返回给 pg_commit 或者我是吗?
基本上我想有一个查询数组并循环它们并使它们查询,然后当所有完成后将它们作为事务提交。
var pg_conn_str = "postgres://postgres:5432@localhost/test2";
var pg = require ('pg');
var rollback = function (client, done) {
client.query ('ROLLBACK', function (err) {
return done (err);
});
};
var queries = ["INSERT INTO foo (bar) VALUES (4)",
"INSERT INTO foo (bar) VALUES (5)"];
pg.connect (pg_conn_str, function (err, client, done) {
if (err) throw err;
client.query ('BEGIN', function (err) {
if (err) return rollback (client, done);
process.nextTick (function() {
if (err)
console.log (err);
async.each (queries, pg_commit, function () {
client.query ('COMMIT', done);
console.log ('done');
});
}); //nextTick
}); //begin
}); //connect
function pg_commit (val) {
client.query (val, function (err) {
if (err) return rollback (client, done);
});
return (val);
}