简体   繁体   中英

Trying to insert data into Postgres with Node.js

I'm trying to send some data to postgres, but I'm getting a error with a column, saying that it doesn't exists. The code:

const { Pool, Client } = require('pg');

const pool = new Pool({
    user: "postgres",
    host: "XXX",
    database: "XXX",
    password: "XXX",
    port: "5432"
});

const inserts = [
    card_amount,
    card_type,
    card_name,
    quantity,
    email,
    "createdAt",
    "updatedAt",

];

let sqlString = `INSERT INTO public.mytable(${inserts})VALUES('12','Type1','MyCard','2', 'myemail', '2020-03-16 16:55:43', '2020-03-16 16:55:43')`
pool.query(sqlString,
    (err, res) => {
        console.log(err, res);
        pool.end();
   }
);

The console says "error: column "createdat" of relation "mytable" does not exist". I've tried with '' instead of "" on createdAt/updatedAt, but doesn't work too, same result.

Do you have these columns in your dbtable. You don't need to insert values in those columns. Try excluding them. createdAt column will have data with insert command and updatedAt will generate value on update command on table automatically by 'pg'. I usuall don't use pg. I use sequelize . You can try, it is database independent also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM