'MongooseError: Callback must be a function, got [object Object]' When using "updateMany" function

Viewed 475

Ignore the messy code. I've been working for hours on end and haven't had time to organize it.

I keep getting this error everytime the messageCount is 100 or more.

const profileData = await Profiles.findOne({
    userID: message.author.id
});
if (!profileData) return console.log('re')
try {
    if (profileData.messageCount >= 100) {
        console.log('yeet')
        await Profiles.updateMany({
            userID: message.author.id
        }, {
            messageCount: 0,
        }, {
            $inc: {
                caseCount: +1
            }
        }, {
            upsert: true
        });
    } else {
        console.log('na')
    }
} catch (error) {
    console.log(error);
}
1 Answers

The updateMany() method needs only 3 parameters, it expects the fourth parameter to be an optional callback function, but you used an object, which explains the error message.

Combine your set and inc parameters like this:

await Profiles.updateMany(
  {
    userID: message.author.id
  },
  {
    $set: { messageCount: 0 },
    $inc: { caseCount: 1 },
  },
  {
    upsert: true
  }
);

Check the docs for more information.

Related