如何解决使用 ColumnSet 在 PG Promise 中运行条件 / For 循环
我试图将一些代码放在一起,这些代码遍历表单上的所有字段,然后将数据插入到要插入的列集中。不过我遇到的问题是 PG-Promise 似乎没有调用函数来运行验证器。知道如何解决这个问题吗?这是我要运行的代码。
exports.detailedbudgetpost = async function (req,res,next) {
const cs = new pgp.helpers.ColumnSet(["attrib_id","value","response_id"],{
table: "formquestionresponse",});
const values = [
{
attrib_id: 20,value: companyDetails.company_name,response_id: insertedForm.response_id,},{
attrib_id: 19,value: companyDetails.first_name + " " + companyDetails.last_name,}
];
const arrayCheck = (values,insertedformresponse) => {
for (var x = 1; x >= 7; x++) {
if (isEmpty(req.body[`goal${x}`]) == false) {
values.push({
attrib_id: 23,value: req.body[`goal${x}`],response_id: insertedformresponse,});
}
if (isEmpty(req.body[`descriptionofneed${x}`]) == false) {
values.push({
attrib_id: 24,value: req.body[`descriptionofneed${x}`],});
}
}
return values;
};
db.tx(async (t) => {
const companyDetails = await db.one(
"SELECT company_name,c.id,first_name,last_name FROM company c INNER JOIN useraccount u on c.id = u.company_id WHERE u.username = $1",[req.user.user]
);
const insertedForm = await db.one(
"INSERT INTO formresponse(company_id,form_id) VALUES ($1,3) RETURNING response_id",[companyDetails.id]
);
arrayCheck(values,insertedForm.response_id);
const query = pgp.helpers.insert(values,cs);
const recordsResponse = await t.none(query);
})
.then((results) => {
req.flash("info","Your form has been succesfully submitted. Thank you!");
res.redirect("/thanks");
})
.catch((error) => {
console.log(error);
req.flash(
"error","An error has occured. Please try again or submit a support ticket."
);
res.redirect("/");
});
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。