0
votes

I'm trying to code a little social media using express and mongoose principally, and I got the error "ERR_HTTP_HEADERS_SENT" while running this code. How am I supposed to fix this ?

const UserModel = require("../models/user.model");
const ObjectID = require("mongoose").Types.ObjectId;


module.exports.updateUser = async (req, res) => {
  if (!ObjectID.isValid(req.params.id))
    return res.status(400).send("ID unknown : " + req.params.id);

  try {
    await UserModel.findOneAndUpdate(
      { _id: req.params.id },
      {
        $set: {
          bio: req.body.bio,
        },
      },
      { new: true, upsert: true, setDefaultsOnInsert: true },
      (err, docs) => {
        if (!err) return res.status(200).json(docs);
        if (err) return res.status(500).json({ message: err });
      }
    );
  } catch (err) {
    return res.status(500).json({ message: err });
  }
};
The error must come from some other part of your code that is executed when you make the problematic request. Can you share that request and also the relevant code? - Heiko Theißen