postgres データベースにデータを挿入するための次の単純なノード アプリケーションがあります。
var pg = require('pg');
var dbUrl = 'tcp://user:psw@localhost:5432/test-db';
pg.connect(dbUrl, function(err, client, done) {
for (var i = 0; i < 1000; i++) {
client.query(
'INSERT into post1 (title, body, created_at) VALUES($1, $2, $3) RETURNING id',
['title', 'long... body...', new Date()],
function(err, result) {
if (err) {
console.log(err);
} else {
console.log('row inserted with id: ' + result.rows[0].id);
}
});
}
});
ターミナルで node app.js を実行すると、データベースに 1000 行が挿入され、アプリケーションがハングして終了しません。私が間違っていることは何ですか?pg モジュールの例を調べましたが、何か違うことをしていることに気付きませんでした…</p>