gpt4 book ai didi

javascript - Node js Postgresql - 插入表时出错

转载 作者:行者123 更新时间:2023-12-03 06:05:12 25 4
gpt4 key购买 nike

我对js很陌生。我想将值插入到错误表中,例如 Node js 中返回的时间戳和错误类型。我当前的 Node js 代码将值插入数据库,但我想要错误 block 中的某些内容。下面是我的 Node.js 代码。

var pg = require('pg');
var transaction = require('pg-transaction');
var die = function(err){
if (err) throw err;
};
exports.handler = function(event, context) {
var pgClient = new pg.Client({
user: 'XXXX',
database: 'XXXX',
password: 'XXXX',
port: 5439,
host: "XXXXX"
});
pgClient.connect();
var tx = new transaction(pgClient);
tx.on('error',die);
tx.begin();
tx.query("truncate table tbl;");
tx.query("insert into tbl..................;");
tx.query("select * from tbl",function(err,result) {
console.log("XXXX: "+JSON.stringify(result));
});
tx.query("truncate table;");
tx.commit(function(){

pgClient.end(function(err) {
if(err) throw err;
console.log("Successful");
});
});
};


插入 error_log(select getdate(),error_type);
那么,我在哪里编写 sql 呢?

最佳答案

正确执行,通过 pg-promise ,带有自动交易:

const pgp = require('pg-promise')(/*initialization options*/);

const db = pgp({
user: 'XXXX',
database: 'XXXX',
password: 'XXXX',
port: 5439,
host: "XXXXX"
});

exports.handler = (event, context) => {
db.tx(t => {
return t.batch([
t.none('truncate table tbl'),
t.none('insert into tbl..................'),
t.any('select * from tbl'),
t.none('truncate table tbl')
]);
})
.then(data => {
console.log("XXXX: ", JSON.stringify(data[2]));
})
.catch(error => {
console.log(error);
});
};

关于javascript - Node js Postgresql - 插入表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39580263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com