0

This is in mean-stack, angular16 JWT authentication part of code:

module.exports = function(app) {
app.use(function(req, res, next) {
res.header(
  "Access-Control-Allow-Headers",
  "Origin, Content-Type, Accept"
);
next();
});
app.get(
"/api/test/mod",
[authJwt.verifyToken, authJwt.isModerator],
controller.moderatorBoard
);

app.get(
"/api/test/admin",
[authJwt.verifyToken, authJwt.isAdmin],
controller.adminBoard
);
}

but MongooseError: Query.prototype.exec() no longer accepts a callback.

I need to convert the code without callbacks. Please help, thank you!

I just don't know how.

1 Answers1

0

Mongoose query exec() method no longer accepts a callback function. This change was introduced in Mongoose version 6.0.0.

In previous versions of Mongoose, you could use exec() with a callback function like this:

const query = MyModel.find({ name: 'John' });
query.exec((err, results) => {
  if (err) {
    // Handle the error
  } else {
    // Process the results
  }
});

However, starting from Mongoose 6.0.0, you should use promises instead of callbacks to handle the query execution. Here's the updated way to use exec():

const query = MyModel.find({ name: 'John' });
query.exec()
  .then((results) => {
    // Process the results
  })
  .catch((err) => {
    // Handle the error
  });

Or, you can use the more modern async/await syntax:

async function getData() {
  try {
    const results = await MyModel.find({ name: 'John' }).exec();
    // Process the results
  } catch (err) {
    // Handle the error
  }
}
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35
  • I changed my code accordingly but get this error:TypeError: query.exec is not a function. can you please help? thank you! – jenniferyu Jul 24 '23 at 19:44