我对js很新。我想在我的错误表中插入值,例如时间戳和节点js中返回的错误类型。我当前的节点js代码将值插入数据库,但我想要一些错误块。下面是我的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(选择getdate(),error_type);
那么,我在哪里编写我的sql?
答案 0 :(得分:1)
通过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);
});
};