gpt4 book ai didi

javascript - NodeJS 发布请求 - 参数中的数字返回 NaN

转载 作者:搜寻专家 更新时间:2023-11-01 00:27:54 24 4
gpt4 key购买 nike

我是 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

Controller :

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

最佳答案

在我看来:

  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;

或者查询参数来自您的请求对象的任何地方。鉴于您正在使用查询参数而不是请求正文中的有效负载调用您的发布请求。

关于javascript - NodeJS 发布请求 - 参数中的数字返回 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51715450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com