使用nodejs在PostgreSql中插入

时间:2015-12-16 09:40:57

标签: javascript node.js postgresql express insert

我有一个应用程序,我通过post从用户那里获取输入,然后在db中插入这些输入。插入整数会导致错误。 integer的输入语法无效。请帮忙。任何帮助将不胜感激。

ws.post('/nextq',function(req,res){

var user_name = req.body.InputName;
var user_add  =  req.body.InputAddress;
var user_mno=req.body.InputMno;
var user_age=req.body.InputAge;
var user_email=req.body.InputEmail;
var user_job=req.body.InputJob;
console.log(user_name + user_add + user_mno + user_age + user_email +  user_job);
if(!(user_name = null) || !(user_add = null) || !(user_mno = null)|| !(user_age = null)|| !(user_email = null)|| !(user_job = null))
{
client.connect(function(err){
if(err){
    return console.error('Could not connect to postgres' , err);
}
client.query("INSERT INTO \"Users\" (\"name\",\"address\",\"mno\",\"email\",\"age\",\"job\") VALUES ('user_name','user_add','user_mno','user_email','user_age','user_job')",function(err,result){if(err) {
  return console.error('error running query', err);
}

1 个答案:

答案 0 :(得分:0)

试试这个

client.query("INSERT INTO Users (name, address, mno, email, age, job) VALUES ($1, $2, $3, $4, $5, $6)", [user_name, user_add, user_mno, user_email, user_age, user_job], function(err,result){if(err) {
  return console.error('error running query', err);
}