我有一个将用户配置文件发布到数据库中的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中给出CANNOT POST
错误和HTML代码。但是,当我从URL中删除:account_id
参数并手动写入50时,请求就可以工作了。这里的查询参数有什么问题?
标头: x- AUTH - TOKEN : AUTH令牌内容类型:应用程序/x-www-form-urlencoded
更新:我得到的错误是:
发布于 2019-06-04 05:32:21
问题是您的后端控制器需要路径中的account_id
参数,而您在查询字符串中提供了它。要使其正常工作,请在Postman中将URL的末尾更改为api/profile/:account_id
,删除查询字符串。此外,该页面还展示了如何在Postman:https://learning.getpostman.com/docs/postman/sending_api_requests/requests/上设置URL参数。
https://stackoverflow.com/questions/56434529
复制相似问题