Collection field showing undefined when aggregate is used in MongoDB

Viewed 26

I'm trying to concat two arrays with aggregate. However, output is showing Cannot read properties of undefined (reading 'allAddresses'). And wanted to implement in EJS. How can I achieve that? There is a code below:

app.post('/indexReplaced.html', (req, res) => {
  Address.find({}, function(err, item){
    const response = Address.aggregate([
      {
        $group: {
          _id: null,
          addresses: {
            $push: "$addresses"
          }
        }
      },
      {
        $project: {
          allAddresses: {
            $reduce: {
              input: "$addresses",
              initialValue: [],
              in: {
                "$concatArrays": [
                  "$$value",
                  "$$this"
                ]
              }
            }
          }
        }
      }
    ]);
    const allAddresses = response[0]["allAddresses"];
    res.render("indexCalculated", {
      address: item,
      allAddresses: allAddresses
    })
  })
});

P.S: Tried to nest aggregate pipeline into Address.find() to render the collection as well. I hope it doesn't affect the pipelane.

0 Answers
Related