【发布时间】:2019-10-19 10:21:46
【问题描述】:
我有一个 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 错误。但是,当我从 URL 中删除 :account_id 参数并手动写入即 50 时,请求有效。这里的查询参数有什么问题?
标题: x-auth-token:授权令牌 内容类型:application/x-www-form-urlencoded
【问题讨论】:
-
您在 Postman 中的配置如何?什么是服务器响应(主要是状态码、正文)?
-
如果有帮助,我已附上屏幕截图。
标签: postgresql express postman