Javascript 如何为pg node js插入未定义的值作为null

Javascript 如何为pg node js插入未定义的值作为null,javascript,node.js,postgresql,Javascript,Node.js,Postgresql,是否有更好的方法使用pg插入未定义为null的变量 const pg = require('pg'); a = a? a: null // hope to remove this line b = b? b: null // hope to remove this line c = c? c: null // hope to remove this line client.query('INSERT INTO abc(a,b,c) VALUES($1,$2,$3)', [a,b,c],

是否有更好的方法使用pg插入未定义为null的变量

const pg = require('pg');
a = a? a: null  // hope to remove this line
b = b? b: null  // hope to remove this line
c = c? c: null  // hope to remove this line

client.query('INSERT INTO abc(a,b,c) VALUES($1,$2,$3)', [a,b,c], function(err, result) { 
  //do something here
})

因此,不必检查每个变量并在

之前用“”替换未定义的变量,您可以使用| |运算符定义未定义的变量。 前


您可以使用类似布尔值的方法检查该值

let test = null;
let b = `test is ${Boolean(test) == false ? "null" : ",'" + test + "'"}`;

显示的代码根本不使用
null
。空字符串不是空的。您是否意识到此代码会将数字
0
和布尔值
false
也替换为
'
?如果您希望删除这3行,那么这行吗<代码>[a | | null,b | | null,c | | null]是否自动执行,未定义的
null
都被格式化为
null
。感谢pg promise看起来很酷,我可以检查pg promise如何处理部分更新吗?如果你有像UPDATE users SET name=$1,age=$2这样的SQL语句,其中id=$3,值为['a',未定义,'id1'],pg是否承诺使用null更新age列?或者忽略年龄进行部分更新?
let test = null;
let b = `test is ${Boolean(test) == false ? "null" : ",'" + test + "'"}`;