在我的React应用程序中,我确实将行添加到数据库(PostgreSQL)中:
addToDatabase() {
fetch(`${'127.0.0.1:3000'}/add`, {
method: 'PUT',
body: JSON.stringify(this.state.person),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
...
}
this.state.person 通常具有下一种格式:
{id: 1, surname: 'Jones', name: 'John', city: 4}
在后端,我使用sequelize将行添加到数据库:
app.route('/add')
.put((req, res) => {
Person.create({
surname: req.body.surname,
name: req.body.name,
city: req.body.city
});
res.send(JSON.stringify({ success: true }));
});
有时我需要添加仅包含姓氏的行。这样, this.state.person 具有下一个格式:
{id: 1, surname: 'Jones'}
但是我得到一个错误:
Unhandled rejection SequelizeDatabaseError: invalid input syntax for integer: ""
后端始终等待所有字段填满。如何添加选择性填充字段的记录?
答案 0 :(得分:0)
req.body.city将设置为未定义,这将导致错误
Use NULL for "empty" values in a numeric column
The empty string '' can only be used in character types like text or varchar