无法访问正文中对象的属性 "undefined"
Accessing attributes of an object in body is not possible "undefined"
我有一个非常奇怪的问题。我正在 Node.js 应用程序中执行 post 请求。我传递了一个表格,其中一个变量是一个对象位置:
{"id":"0ba78ba1-c7dc-440f-b166-4b2cd6dceff0","coordinates":{"crs":{"type":"name","properties":{"name":"EPSG:4326"}},"type":"Point","coordinates":[25.28827329151016,54.69353842802546]},"place_name":"Viva Pizza & Sushi & Wok ","description":"Sushi and pizza based fast food restaurant","category":"Food","createdAt":"2021-03-17T13:25:27.619Z","updatedAt":"2021-03-17T13:25:27.619Z"}.
问题是我可以在我的函数中以 req.body.place 的形式访问它,但是当我尝试获取
req.body.place.id
id 未定义。相反,以相同的方式访问 req.body.user.id 就可以了。我在这里做错了什么?访问对象属性似乎非常简单,但在这种情况下不起作用。任何建议都会有所帮助。
更多代码如下:
后端函数:
exports.likePlace = async function (req, res) {
const user = await models.User.findOne({
where: {
id: req.user.id
}
});
const place = await models.Place.findOne({
where: {
id: req.body.place.id
}
});
try {
await user.addPlace(place, { through: { selfGranted: false } });
res.send(true);
}
catch (e) {
console.log(e);
res.send(false);
}
}
发出 post 请求:
input(type="hidden" name="place" value=places[0])
button#nope(type="submit")
只需在您的输入中使用 id 之类的值:
input(type="hidden" name="place" value=places[0].id)
button#nope(type="submit")
// ...code
const place = await models.Place.findOne({
where: {
id: req.body.place
}
});
// ...code
我有一个非常奇怪的问题。我正在 Node.js 应用程序中执行 post 请求。我传递了一个表格,其中一个变量是一个对象位置:
{"id":"0ba78ba1-c7dc-440f-b166-4b2cd6dceff0","coordinates":{"crs":{"type":"name","properties":{"name":"EPSG:4326"}},"type":"Point","coordinates":[25.28827329151016,54.69353842802546]},"place_name":"Viva Pizza & Sushi & Wok ","description":"Sushi and pizza based fast food restaurant","category":"Food","createdAt":"2021-03-17T13:25:27.619Z","updatedAt":"2021-03-17T13:25:27.619Z"}.
问题是我可以在我的函数中以 req.body.place 的形式访问它,但是当我尝试获取
req.body.place.id
id 未定义。相反,以相同的方式访问 req.body.user.id 就可以了。我在这里做错了什么?访问对象属性似乎非常简单,但在这种情况下不起作用。任何建议都会有所帮助。
更多代码如下:
后端函数:
exports.likePlace = async function (req, res) {
const user = await models.User.findOne({
where: {
id: req.user.id
}
});
const place = await models.Place.findOne({
where: {
id: req.body.place.id
}
});
try {
await user.addPlace(place, { through: { selfGranted: false } });
res.send(true);
}
catch (e) {
console.log(e);
res.send(false);
}
}
发出 post 请求:
input(type="hidden" name="place" value=places[0])
button#nope(type="submit")
只需在您的输入中使用 id 之类的值:
input(type="hidden" name="place" value=places[0].id)
button#nope(type="submit")
// ...code
const place = await models.Place.findOne({
where: {
id: req.body.place
}
});
// ...code