【发布时间】:2021-06-30 19:46:13
【问题描述】:
我想捕获无效的 json。例如,如果我收到"usern": "James" 而不是"username": "James"。我想回复400 状态码。
exports.create_user = async (req, res) => {
const { username } = req.body;
try {
const user = await User.create({
username: username,
});
res.status(201).json(user);
} catch (error) {
res.status(503).end();
}
res.status(400).json({ msg: "Syntax error" });
};
现在这并不能捕获无效的 json,我该怎么做?
【问题讨论】:
标签: javascript node.js mongodb express try-catch