使用 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 (将#修改为@)