【发布时间】:2021-07-17 07:07:05
【问题描述】:
express.json() 没有解析嵌套的 json 对象。它返回 [object Object]。
express - 4.17.1,
请求标头
Content-Type: application/json;charset=UTF-8
我使用 express.json() 如下。
app.use(express.json());
当我发布没有嵌套对象的数据时。它有效
const data = await this.$axios.$post(`user/update/${this.username}`, {confirmationCode: "code"});
现在 req.body.confirmationCode 是“代码”。
但如果我发布嵌套的 json 对象。它不起作用
const data = await this.$axios.$post(`user/update/${this.username}`, {confirmationCode: { mobile: "code"}});
现在 req.body.confirmationCode.mobile 未定义。 req.body.confirmationCode 是 [object Object]。
【问题讨论】:
-
看起来您正在将
req.body.confirmationCode转换为字符串。req.body.confirmationCode.toString()->[object Object]. -
无法复制。