Node.js 有没有办法在nodejs和postgresql中解决这个问题?
我正试图将一些数据发送到PostgreSQL数据库,但出现以下错误:Node.js 有没有办法在nodejs和postgresql中解决这个问题?,node.js,postgresql,express,Node.js,Postgresql,Express,我正试图将一些数据发送到PostgreSQL数据库,但出现以下错误: connected to the database { error: null value in column "firstname" violates not-null constraint at Connection.parseE (F:\expressApi\node_modules\pg\lib\connection.js:604:11) at Connection.parseMessage (F:\e
connected to the database
{ error: null value in column "firstname" violates not-null constraint
at Connection.parseE (F:\expressApi\node_modules\pg\lib\connection.js:604:11)
at Connection.parseMessage (F:\expressApi\node_modules\pg\lib\connection.js:401:19)
at Socket.<anonymous> (F:\expressApi\node_modules\pg\lib\connection.js:121:22)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
name: 'error',
length: 214,
severity: 'ERROR',
code: '23502',
detail: 'Failing row contains (6, null, null, null, null, null).',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
userService.js
有什么帮助吗?数据库列没有null约束,您正在向它输入null值。我可以看看您使用
create
功能的代码吗。您是否也将数据作为JSON发送到端点???根据您的表定义,firstname不能为null,您正在尝试将null传递给它。这是我的创建代码:我在userService.js文件create(body,(err,results)
正上方的userController.js文件中使用了create函数
const { create } = require('./user.service');
//const { genSaltSync, hashSync } = require('bcrypt');
module.exports = {
createUser: (req, res) => {
const body = req.body;
console.log(body)
// const salt = genSaltSync(10);
//body.password = hashSync(body.password, salt);
create(body, (err, results) => {
if (err) {
console.log(err);
return res.status(500).json({
success: 0,
message: "Database connection error"
});
}
return res.status(200).json({
success: 1,
data: results
})
})
}
}
const pool = require('../../config/database');
pool.on('connect', () => {
console.log('connected to the database');
})
module.exports = {
create: (data, callBack) => {
pool.query(
// 'insert into registration(firstname, lastname, gender, email, password) values($1, $2, $3, $4, $5)'
`insert into registration(firstname, lastname, gender, email, password)
values($1, $2, $3, $4, $5)`, [
data.first_name,
data.last_name,
data.gender,
data.email,
data.password
],
(error, results, fields) => {
if (error) {
return callBack(error);
}
return callBack(null, results);
}
);
}
}