You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过Axios向Express路由传递布尔值失效问题求助

Fixing Your Switch Statement Issue with Route Parameters

Hey there! Let's break down why your switch statement is always hitting the default branch, no matter what toggle value you pass.

The Root Problem

Route parameters (like req.params.toggle) are always strings—even if you pass true or false in the URL. When you compare the string "true" to the boolean true in your case true: clause, JavaScript uses strict equality (===), which checks both value and type. Since a string and a boolean are different types, the match fails every time, and your code falls into the default branch.

This is also why your earlier if(req.params.toggle) attempt didn't work: in JavaScript, any non-empty string is considered "truthy"—so even the string "false" would pass the if check, leading to unexpected behavior.

Solutions to Fix This

Option 1: Match Against String Values

Update your case clauses to use string literals instead of booleans, since that's what req.params.toggle actually is:

router.post('/team_activate/:team_oid/:toggle', function(req, res) {
  console.log("req.params.toggle: "+req.params.toggle)
  switch (req.params.toggle){
    case "true":
      console.log("Live")
      Team.findOneAndUpdate({ _id: req.params.team_oid},{"$set": { "active": true, "status": "Live" }}).exec(function(err, doc){
        if(err){
          return res.status(500).send(err); // Better error handling than throw
        } else {
          res.send(doc)
        }
      })
      break;
    case "false":
      console.log("Paused")
      Team.findOneAndUpdate({ _id: req.params.team_oid},{"$set": { "active": false, "status": "Paused" }}).exec(function(err, doc){
        if(err){
          return res.status(500).send(err);
        } else {
          res.send(doc)
        }
      })
      break;
    default:
      console.log("Invalid toggle value")
      res.status(400).send("Toggle must be 'true' or 'false'");
      break;
  }
})

Option 2: Convert the Parameter to a Boolean First

If you prefer working with boolean values, convert the string parameter to a boolean before using it in the switch:

router.post('/team_activate/:team_oid/:toggle', function(req, res) {
  console.log("req.params.toggle: "+req.params.toggle)
  // Convert string to boolean: only "true" becomes true, everything else is false
  const isActive = req.params.toggle === "true";
  
  switch (isActive){
    case true:
      console.log("Live")
      Team.findOneAndUpdate({ _id: req.params.team_oid},{"$set": { "active": true, "status": "Live" }}).exec(function(err, doc){
        if(err){
          return res.status(500).send(err);
        } else {
          res.send(doc)
        }
      })
      break;
    case false:
      console.log("Paused")
      Team.findOneAndUpdate({ _id: req.params.team_oid},{"$set": { "active": false, "status": "Paused" }}).exec(function(err, doc){
        if(err){
          return res.status(500).send(err);
        } else {
          res.send(doc)
        }
      })
      break;
  }
})

Extra Tips

  • Avoid throw(err) in Express routes: Throwing errors can crash your server. Instead, send an HTTP 500 response with the error details.
  • Return updated documents: Add { new: true } as a third argument to findOneAndUpdate if you want the response to include the updated document (by default, it returns the document before changes).
  • Validate parameters early: Add a check at the start to ensure req.params.toggle is only "true" or "false"—this helps catch invalid inputs before running database queries.

内容的提问来源于stack exchange,提问作者Izzi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 22:22:29