我对邮递员JSON发布要求的语法是:
{
"id": 3,
"type": "Colonial",
"Location": 1.1,
"Postnumber": 1424,
"City": "Helsinki",
"Reservation_time": "1112"
}
These column hold values in postgresDB
id = serial
type = varchar
location = point
postnumber = int
City = varchar
Reservation time = varchar
我无法弄清楚什么是错误的,因为我得到的错误是整数的错误类型。
编辑:添加了端点模型和服务。
型号:
exports.createTila = async (id, type, location, postnumber, city, reservation_time) => {
try {
const result = await client.query(
"INSERT INTO tila (id, type, location, postnumber, city, reservation_time) VALUES$
[id, type, location, postnumber, city, reservation_time]
);
return result.rows;
} catch (err) {
throw new Error(err);
}
};
服务:
exports.createTila = async (request, response) => {
try {
const tilaObject = request.body;
const newTila = await tilaModel.createTila(tilaObject);
response.status(200).send(newTila);
} catch (err) {
response.status(400).send({ message: err.message });
}
};
答案 0 :(得分:0)
检查表字段的值,任何字段值都将为null,这就是它引发此异常的原因。并更改字段
{
"id": 3,
"type": "Colonial",
"latitude": 1.1,
"longitude " : 1.1
"postnumber": 1424,
"city": "Helsinki",
"reservation time": "1112"
}
这里的纬度和经度是双精度类型
答案 1 :(得分:0)
我认为问题出在point
类型的位置,您的json应该具有latitude
和longitude
值,而不是像1.1这样的浮点数
{
"id":3,
"type":"Colonial",
"Location":{
"latitude":35.55689,
"longitude":51.54865
},
"Postnumber":1424,
"City":"Helsinki",
"Reservation time":"1112"
}
答案 2 :(得分:0)
我发现了我的错误:
不是像这样在服务中创建对象
exports.createTila = async (request, response) => {
try {
const tilaObject = request.body;
const newTila = await tilaModel.createTila(tilaObject);
response.status(200).send(newTila);
} catch (err) {
response.status(400).send({ message: err.message });
}
};
我还需要在服务中定义db列的值,如下所示:
exports.createTila = async (request, response) => {
try {
const {id, type, location, postnumber, city, reservation_time} = request.body;
const newTila = await tilaModel.createTila(id, type, location, postnumber, city, reservation_time);
response.status(200).send(newTila);
} catch (err) {
response.status(400).send({ message: err.message });
}
};