var Integer = require('integer'); client.query('INSERT INTO users (id, first_name, last_name, email, password, phone_number) VALUES ($1, $2, $3, $4, $5, $6)', [Integer(), req.body.firstName, req.body.lastName, req.body.username, pwd, req.body.phoneNumber]
This is my function where I trying to post id to my postgreqsl table. I installed integer for node by npm, set integer data type for my id column in pg and it does not work when i was trying to put it into the database.
I had a such error:
{ error: invalid input syntax for integer: "{"high":0,"low":0}"
When I imported uuivdv4 to the file and changed the Integer() by uuidv4(),(of course i remember about changing data type in id column) i had a good insert.
const uuidv4 = require('uuid/v4'); client.query('INSERT INTO users (id, first_name, last_name, email, password, phone_number) VALUES ($1, $2, $3, $4, $5, $6)', [uuidv4(), req.body.firstName, req.body.lastName, req.body.username, pwd, req.body.phoneNumber]
integer
package is not intended to generate a random integer number like that of uuid/v4
, which generates a random UUID string. The integer is meant for converting a string literal to number eg '123'
to 123
.
The below will work for you.
Updated code:
let randNumber= Math.floor(Math.random() * 10000000000);
client.query('INSERT INTO users (id, first_name, last_name, email, password, phone_number) VALUES ($1, $2, $3, $4, $5, $6)', [randNumber, req.body.firstName, req.body.lastName, req.body.username, pwd, req.body.phoneNumber]
EDIT : Change the column Id
in your users to auto-increment
and use the below query to insert.
client.query('INSERT INTO users (first_name, last_name, email, password, phone_number) VALUES ($1, $2, $3, $4, $5)', [req.body.firstName, req.body.lastName, req.body.username, pwd, req.body.phoneNumber]
After you have changed it to autoincrement
, no need to specify the id
for inserting.
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.