Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
Mongoose 'Query was already executed' error

As the name states, I keep getting a "Query was already executed" while running Mongoose.find queries. Using '.clone' does not seem to be fixing the issue...

My calling code is:

  let result = mongo.isValidUsername(req.body.username).then((data) => {
    return data;
  });
  if ((await result) == false) {
    res.send("Sorry, that username is unavailable");
  } else {
    mongo
      .addUser(
        req.body.username,
        req.body.password,
        req.body.firstName,
        req.body.lastName,
        req.body.email,
        req.body.phoneNumber
      )
      .then(() => {
        let profileData = mongo.getProfileData(req.body.username);
        profileData
          .then((data) => {
            res.render("accountDisplay", {
              results: data,
              trans: [9.99],
            });
          })
          .catch((err) => {
            console.log(err);
          });
      });
  }

I call a query twice - Once in isValidUsername() at the beginning (where I have not used .clone) and then again in getProfileData( where I HAVE used .clone).

I keep getting this error. Any idea what could be causing it?

Here is the code for isValidUsername() and getProfileData(), just in case...

async function isValidUsername(usernameToQuery) {
  //connect to mongoose database
  mongoose.connect("mongodb://localhost:27017/bankDB");
  try {
    let isValid = UserModel.findOne({ username: usernameToQuery }).then(
      (data) => {
        if (data == null) {
          return true;
        } else {
          return false;
        }
      }
    );
    return await isValid;
  } catch (err) {
    return err;
  }
}

async function getProfileData(usernameToQuery) {
  mongoose.connect("mongodb://localhost:27017/bankDB");
  let profileData = UserModel.findOne({ username: usernameToQuery }).clone();
  console.log(await profileData);
  let profileArray = await profileData.then((data) => {
    return [
      data._doc.firstName,
      data._doc.lastName,
      data._doc.email,
      data._doc.phoneNumber,
    ];
  });
  return await profileArray;
}

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!