私は2つのテーブル "A"と "B"を持っています。私はテーブル "A"の主キーを含むテーブル "B"に行を作成したい、この全体の操作はアトミックでなければなりません。pg-promisesの子の約束から親の約束を守る方法
function test(data, res) {
let query1 = knex.insert([data], "id").into("A").toString();
let query2 = "";
db.tx(function (t) {
return this.batch([
t.one(query1).then(function (id) {
query2 = knex.insert({A_id:id, x:x, y:y}).into("B").toString();
t.none(query2).catch(function (error) {
console.log(error); // want to pass this error to next catch block
});
})
]);
}).then(function() {
console.log("success");
}).catch(function (error) {
console.log(error);
});
}
エラーがネストされた約束に来るたびに、私は親の約束を拒否し、親の約束にそのエラーを渡したいと思います。
あなたが子供の中で建設的なことをしていない限り、 'catch()'はそれを削除するだけです。あなたが約束を返す限り、親キャッチは起動しますが、 't.one'の中に' t.none'も返す必要があります。 – charlietfl
@charlietfl私は試しましたが、エラー "Unhandled promise rejection"を取得できませんでした。 – Naresh
あなたのトランザクションに 't.batch'を使用することは絶対にありません。 'pg-promise'はそれ自身で、さらに強力な挿入と更新をサポートしていますので、' knex'を使う必要はありません。 –