我刚接触NodeJS。尝试发出POST请求,但参数中的所有整数都以NaN的形式传递。在下面的第二行,您 可以在请求中看到将实际数字分配给了参数,但是在发生发布请求的第一行中,所有数字均为NaN。我在这里想念什么?
2018-08-06T20:44:24.391354+00:00 app[web.1]: Executing (default): INSERT INTO "public"."meetings" ("id","requestor_id","requestee_id","lang_one_id","lang_two_id","meeting_time","accepted","status","requestor_score","requestee_score") VALUES (DEFAULT,NaN,NaN,NaN,NaN,'2018-08-06 20:44:24.000 +00:00',false,'Active',0,0) RETURNING *;
2018-08-06T20:44:24.401308+00:00 app[web.1]: POST /api/meetings/createMeeting?requestor_id=32&requestee_id=1&meeting_time=2018-08-16%2013:00:00&location=NYC&location_address=NYC&lang_one_id=1&lang_two_id=2 500 59.324 ms - 1272
控制器:
meetings.createMeeting = (req, res) => {
const { requestor_id, requestee_id, meeting_time, accepted, location, location_address, status, lang_one_id, lang_two_id } = req.body;
let data = {
requestor_id: parseInt(requestor_id),
requestee_id: parseInt(requestee_id),
meeting_time: Date(meeting_time),
accepted: false,
location,
location_address,
status: "Active",
lang_one_id: parseInt(lang_one_id),
lang_two_id: parseInt(lang_two_id),
requestor_score: parseInt(0),
requestee_score: parseInt(0),
created_at: Date.now(),
updated_at: Date.now()
};
models.Meetings.create(data)
.then(response => {
res.status(200).json({ status: 200, message: response });
})
.catch(error => {
res.status(500).json({ status: 500, err: error });
});
};
当我更改为req.params
时,没有任何数据:
2018-08-06T21:11:21.830182+00:00 app[web.1]: Executing (default): INSERT INTO "public"."meetings" ("id","requestor_id","requestee_id","lang_one_id","lang_two_id","meeting_time","accepted","status","requestor_score","requestee_score") VALUES (DEFAULT,NaN,NaN,NaN,NaN,'2018-08-06 21:11:21.000 +00:00',false,'Active',0,0) RETURNING *;
2018-08-06T21:11:21.849573+00:00 app[web.1]: POST /api/meetings/createMeeting?requestor_id=32&requestee_id=1&meeting_time=2018-08-16%2013:00:00&location=NYC&location_address=NYC&lang_one_id=1&lang_two_id=2 500 51.120 ms - 1272
答案 0 :(得分:4)
对我来说像:
const { requestor_id, requestee_id, meeting_time, accepted, location, location_address, status, lang_one_id, lang_two_id } = req.body;
应该是:
const { requestor_id, requestee_id, meeting_time, accepted, location, location_address, status, lang_one_id, lang_two_id } = req.params;
或者查询参数来自请求对象的任何地方。假设您要使用查询参数而不是请求正文中的有效负载来调用发布请求。