【问题标题】:If sentence in Express/Node dosent return value如果 Express/Node 中的语句不返回值
【发布时间】:2022-01-31 13:26:25
【问题描述】:

想法

我制作了第二张表,用于存储有关客户的特殊信息。 customer_Id 是我的客户表的外键。但是,根据我的反应,我有一个表格,根据该表格中的信息,我需要发送不同的查询。我尝试了一个 if 语句,它使用正确的信息将该行添加到表中,但它没有将 newReq 设置为返回的内容。当我将它移到 if 语句之外时,它可以正常工作并返回正确的信息。是否有解决此问题的方法,或者我该如何以更好的方式进行此操作?

  try {
    if (req.body.type === "allergy") {
      console.log(req.params.id)
      const newReq = await db.query('INSERT INTO specialcustomerinfo(customer_id,type,allergicto,dangerscale,priority) values($1,$2,$3,$4,$5) returning *',[req.params.id, req.body.type, req.body.allergicto, req.body.dangerscale, req.body.priority]);
    }
    else if (req.body.type === "scaredof") {
      const newReq = await db.query('INSERT INTO specialcustomerinfo(customer_id,type,freight,priority) values($1,$2,$3,$4) returning *',[req.params.id, req.body.type, req.body.freight, req.body.priority]);
    }
    else if (req.body.type === "medicine") {
      const newReq = await db.query('INSERT INTO specialcustomerinfo(customer_id,type,medicineName,priority) values($1,$2,$3,$4) returning *',[req.params.id, req.body.type, req.body.medicineName, req.body.priority]);
    }
    else {
      console.log("No type match")
    }
    res.status(201).json({
      status: "success",
      data: {
        bonus: newReq.rows[0],
      },
    });
  } catch (err) { console.log(err) }
})```

【问题讨论】:

    标签: node.js reactjs postgresql express


    【解决方案1】:

    constlet 在 Javascript 中是块作用域的。这意味着它们只能在声明它们的块内访问。因此,您声明的所有不同版本的const newReq 只能在其块内使用。相反,请在函数的更高级别使用 let 声明变量,以便您可以在需要的地方使用它。

      try {
        let newReq;
        if (req.body.type === "allergy") {
          console.log(req.params.id)
          newReq = await db.query('INSERT INTO specialcustomerinfo(customer_id,type,allergicto,dangerscale,priority) values($1,$2,$3,$4,$5) returning *',[req.params.id, req.body.type, req.body.allergicto, req.body.dangerscale, req.body.priority]);
        }
        else if (req.body.type === "scaredof") {
          newReq = await db.query('INSERT INTO specialcustomerinfo(customer_id,type,freight,priority) values($1,$2,$3,$4) returning *',[req.params.id, req.body.type, req.body.freight, req.body.priority]);
        }
        else if (req.body.type === "medicine") {
          newReq = await db.query('INSERT INTO specialcustomerinfo(customer_id,type,medicineName,priority) values($1,$2,$3,$4) returning *',[req.params.id, req.body.type, req.body.medicineName, req.body.priority]);
        }
        else {
          console.log("No type match");
          res.sendStatus(401);
          return;
        }
        res.status(201).json({
          status: "success",
          data: {
            bonus: newReq.rows[0],
          },
        });
      } catch (err) { 
         console.log(err);
         res.sendStatus(500); 
      }
    })
    

    仅供参考,您还需要在代码进入您的else 子句时添加一个修复程序,因为当您尝试引用newReg.rows 时会抛出该错误。可能,您应该在 else 子句中发送一些错误状态,然后是 return,这就是我所展示的。

    您还需要在 catch 中发送错误响应,因为通过您的函数的所有路径都需要发送某种响应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 2014-01-15
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      相关资源
      最近更新 更多