1

I have this code:

router.post('/BWreport', async function(req, res, next){
     try{
        var cname = await corporate.find().select('corporateID username');
        cname.forEach(function(cname, await){
          cname["totalcases"] = 1;
        });
        return res.json({ response:true, data:cname });
      }catch(err){
        return res.json({ response:false, data:err  });
      }
});

As you can see in the output, totalcases is not getting added:

    "response": true,
    "data": [
        {
            "_id": "61d3f90340b6cad5974ca50a",
            "username": "SB Demo",
            "corporateID": "SB00001"
        },
        {
            "_id": "62266f20fa0eb7a8fbe7d82a",
            "corporateID": "SB00002",
            "username": "NeoGrowth"
        }]

as you can see new key/value pair not getting in output, can anyone tell me why? desired output below

    "response": true,
    "data": [
        {
            "_id": "61d3f90340b6cad5974ca50a",
            "username": "SB Demo",
            "corporateID": "SB00001",
            "totalcases": "1"
        },
        {
            "_id": "62266f20fa0eb7a8fbe7d82a",
            "corporateID": "SB00002",
            "username": "NeoGrowth",
            "totalcases": "1"
        }]
Codebling
  • 10,764
  • 2
  • 38
  • 66
  • Which database are you using? Mongodb? – Drashti Kheni Nov 04 '22 at 07:14
  • 3
    First problem is that you are using a keyword as a variable name, and reusing a variable name `function(cname, await)` – Codebling Nov 04 '22 at 07:14
  • 2
    @vighnesh153 `await` is a reserved keyword so that also needs to be changed – Codebling Nov 04 '22 at 07:19
  • 1
    Not entirely https://stackoverflow.com/questions/55934490/why-are-await-and-async-valid-variable-names (though in the circumstances that it appears it definitely deserves to be refactored out) – CertainPerformance Nov 04 '22 at 07:24
  • @CertainPerformance that is weird. As I was writing it, I thought to myself that it's weird that a new feature could introduce reserved words. Thanks for the correction. My statement was obviously wrong anyway since OP appeared to be using said code – Codebling Nov 04 '22 at 07:26

2 Answers2

2

Mongoose documents are immutable. Use .lean() to make it return a Javascript object that can be modified:

router.post('/BWreport', async function(req, res, next){
     try{
        var cname = await corporate.find().select('corporateID username').lean();

        cname.forEach(function(c, await){
          c["totalcases"] = 1;
        });

        return res.json({ response:true, data:cname });
        }catch(err){
        return res.json({ response:false, data:err  });
      }
});
Codebling
  • 10,764
  • 2
  • 38
  • 66
Drashti Kheni
  • 1,065
  • 9
  • 23
1
router.post('/BWreport', async function(req, res, next){
 try{
    var cname = await corporate.find().select('corporateID username');
    cname.forEach((elem, ind, arr) => {
      arr[ind]['totalcases'] = 1;
    });
    return res.json({ response:true, data:cname });
  }catch(err){
    return res.json({ response:false, data:err  });
  }
});
Syed Jawad
  • 51
  • 6