Home > OS >  My code is working with mongoose 5.10.6 but not with the latest version of it
My code is working with mongoose 5.10.6 but not with the latest version of it

Time:10-07

I have this function which updates preference in the mongoDB database.

async function updateUserPreference(req, res) {
  if (!ObjectId.isValid(req.params.id))
    return res.status(400).send("ID unknown : "   req.params.id);

  try {
    await userModel.findByIdAndUpdate(
      req.params.id,
      { $addToSet: { preference: req.body.preference } },
      { new: true },
      (err, docs) => {
        if (!err) return res.status(201).json(docs);
        else return res.status(400).send(err);
      }
    );
  } catch (err) {
    return res.status(500).json({ message: err });
  }
}

I found out on this topic that it works with mongoose version 5.10.6 (indeed it does). Do you know why ?

This is my db.js file :

import mongoose from "mongoose";
import dotenv from "dotenv";
dotenv.config({ path: "./config/.env" });

mongoose
  .connect(
    "mongodb srv://"  
      process.env.passwordDB  
      "@cluster0.ecf8q.mongodb.net/testTech",
    {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useCreateIndex: true,
      useFindAndModify: false,
    }
  )
  .then(() => console.log("connected"))
  .catch((err) => console.log("Failed to connnect to DB", err));

export default mongoose;

When I was using the latest version of mongoose I removed useCreateIndex: true and useFindAndModify: false

In postman I have this error :

{
    "message": {
        "originalStack": "Error\n    at model.Query._wrappedThunk [as _findOneAndUpdate] (D:\\Programmation\\mern\\node_modules\\mongoose\\lib\\helpers\\query\\wrapThunk.js:25:28)\n    at D:\\Programmation\\mern\\node_modules\\kareem\\index.js:279:20\n    at _next (D:\\Programmation\\mern\\node_modules\\kareem\\index.js:103:16)\n    at D:\\Programmation\\mern\\node_modules\\kareem\\index.js:508:38\n    at processTicksAndRejections (internal/process/task_queues.js:75:11)"
    }
}

And in console I have :

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:561:11)
    at ServerResponse.header (/Users/hugobeheray/Documents/JEECE/TestTechniqueJEECE/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/Users/hugobeheray/Documents/JEECE/TestTechniqueJEECE/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/Users/hugobeheray/Documents/JEECE/TestTechniqueJEECE/node_modules/express/lib/response.js:267:15)
    at file:///Users/hugobeheray/Documents/JEECE/TestTechniqueJEECE/controllers/user.controller.js:35:42
    at /Users/hugobeheray/Documents/JEECE/TestTechniqueJEECE/node_modules/mongoose/lib/model.js:4919:18
    at processTicksAndRejections (internal/process/task_queues.js:77:11)
Emitted 'error' event on Function instance at:
    at /Users/hugobeheray/Documents/JEECE/TestTechniqueJEECE/node_modules/mongoose/lib/model.js:4921:15
    at processTicksAndRejections (internal/process/task_queues.js:77:11) {
  code: 'ERR_HTTP_HEADERS_SENT'
}

I can work with mongoose 5.10.6 but I would like to know why this doesn't work !

CodePudding user response:

I got this error yesterday. It's because you are sending 2 responses to the client at the same time.

  • Related