我有一条Express路由,可将用户个人资料发布到数据库中
router.post(
"/:account_id",
[
auth,
[
check("name", "Please enter your name")
.not()
.isEmpty(),
check("orgName", "Please enter your organization name")
.not()
.isEmpty()
]
],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
try {
const accountId = req.params.account_id;
const { name, orgName } = req.body;
// Check for the existing profile
let query = await db.query(
`SELECT 1 FROM profile WHERE profile.account_id='${accountId}'`
);
if (query.rowCount !== 0) {
return res.status(400).send({
errors: [
{
msg: "You have already created your profile"
}
]
});
}
// Insert the profile
query = await db.query(
`INSERT INTO profile (
account_id, profile_name, company_name) VALUES ('${accountId}', '${name}', '${orgName}') returning profile_id, account_id, profile_name, company_name`
);
const profile = query.rows[0];
return res.json(profile);
} catch (err) {
console.error(err.message);
res.status(500).send("Server error");
}
}
);
此请求无效,并且在Postman中使用HTML代码给出了CANNOT POST
错误。但是,当我从网址中删除:account_id
参数并手动写入50时,请求有效。这里的查询参数有什么问题?
标题: x-auth-token:AUTH TOKEN 内容类型:application / x-www-form-urlencoded
答案 0 :(得分:3)
问题是后端控制器在路径中需要account_id
参数,而您在查询字符串中提供了该参数。要使其正常工作,请在Postman中将URL的末尾更改为api/profile/:account_id
,并删除查询字符串。另外,此页面还显示了如何在Postman上设置URL参数:https://learning.getpostman.com/docs/postman/sending_api_requests/requests/