'how do I send email to all the users that were returned by a mongoose find query

I am trying to send 2 different emails once the user is registered.

  1. first email to the registered user
  2. the second email to the registered user's team manager and teammates

I'm trying to get the email ids of the team manager and their teammates using a find query. but how do I retrieve just their email id and then use those ids in the "to" section of the email?

here's my code

Employee.findOne({ empEmail: empEmail })
.then((userExist) => {
  if (userExist) {
    return res.status(422).json({ error: 'Email already exists' });
  }
  const employee = new Employee({ empFirstName, empLastName, empEmail, empPassword, empConfirmPass, empContactNum, empPosition, empTeam, gender });

  employee.save()
    .then(() => {
      var mailOptions = {
        from: '[email protected]',
        to: employee.empEmail,
        subject: 'Sign up success!',
        text: "Hi " + employee.empFirstName + " you have been successfully registered. Below are your credentials to log in to our system. "+
        "username: "
          + employee.empEmail +
        " password: " + empPassword + " "
      };
      transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
          console.log(errror);
        } else {
          console.log('email sent:' + info.response);
        }
      });
      res.status(201).json({ message: "user successfully registered" });
    })
    .then(()=>{
      Employee.find({empTeam: empTeam})
      .then(()=>{
        var mailOptions2 = {
          from: '[email protected]',
          to: ;//all the emails from the same team as the user
          subject: 'Welcoming [Employee’s name] to [Company name] / [department]',
          text: "Hello team, I am pleased to introduce [employee's full name], who is joining us at [company's name] as a [job title]."
        };
        transporter.sendMail(mailOptions, function (error, info) {
          if (error) {
            console.log(errror);
          } else {
            console.log('email sent:' + info.response);
          }
        });
      })
    })
    .catch(err => res.send(err.message));
}).catch((err) => { console.log(err) });


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source