Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert undefined value as null for pg node js

Is there a better method of inserting a variable that is undefined as null using pg

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
})

so that don't have to check every variable and substitute "" for undefined before

like image 456
Stanley Avatar asked May 17 '26 21:05

Stanley


1 Answers

you can define a undefined variable using || operator. Ex

var a ;
var b = a || null;
like image 119
Naveen Kerati Avatar answered May 20 '26 11:05

Naveen Kerati