'How to fix the error (TypeError: Cannot assign to read only property 'map' of object '#<QueryCursor>')
I am sending my data to MongoDB via Mongoose. Now, during the fetch of API route for it, an error is thrown.
Code
const addChoice = async (e) => {
try {
e.preventDefault();
const res = await fetch("/api/sendChoice", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
choiceSeq: choice,
checkSubmit: true,
}),
});
console.log(res);
router.push("/home");
} catch (error) {
console.log(error);
}
};
The error is happening at const res = await fetch("/api/sendChoice" ,{
In terminal server the error
error - TypeError: Cannot assign to read only property 'map' of object '#<QueryCursor>'
In the inspect element the error is as
I can't find anything related to fix this issue, I don't even understand what the error means to try to resolve it myself.
Some other related code from my project:
api/sendChoice
import { getSession } from "next-auth/client";
import dbConnect from "../../helpers/dbConnect";
import Choice from "../../models/Choice";
export default async function sendChoice(req, res) {
try {
const session = await getSession({ req });
await dbConnect();
if (!session) {
res.status(401).send("You are not signed in");
return;
}
if (req.method === "POST") {
console.log(req.body);
const { choiceSeq, checkSubmit } = req.body;
console.log(choiceSeq, checkSubmit);
const userId = session.user.id;
const nameP = session.user.name;
const choice = new Choice({
user: userId,
name: nameP,
choiceSeq,
checkSubmit,
});
await choice.save();
res.status(200).send("Choice saved");
} else {
res.status(400).send("Bad request");
}
}
catch (error) {
console.log(error);
}
}
The MongoDB schema
import mongoose, { Schema } from 'mongoose';
const ChoiceSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'User',
},
name: {
type: String,
},
choiceSeq: {
type: Array,
default: [],
},
checkSubmit: {
type: Boolean,
}
});
mongoose.models = {};
export default mongoose.model('Choice', ChoiceSchema);
Solution 1:[1]
This issue occured recently and apparently its happening with latest version of node.
So you can change the version of node to older version and it will be fixed. I am using node version v14.19.0
Solution 2:[2]
if you are using Docker then, giving a version will solve the problem.
before:
FROM node:alpine
now
FROM node:16.5.0-alpine
WORKDIR /app
COPY package.json .
RUN npm install --only=prod
COPY . .
CMD ["npm", "start"]
Solution 3:[3]
The latest release of Node.JS is what is causing this issue. In your package.json, make sure to set your engine to"engines": { "node": ">=0.12 < 17.5.0" } and you should be fine.
Also if you are using docker for deployment, make sure to change the version number in your dockerfile to be less that 17.5.0
Solution 4:[4]
Solution which worked well for me:)
Step 01: Open your terminal and copy paste below command.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | zsh
Wait patiently until its done.
Step 02: sudo vim ./zshrc
Step 03: Press I for Insert Mode and copy paste below command. Must be same in three lines.
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
then press ESC key :wq (write and quite).
Step 04: brew install nvm
step 05: nvm install node (which will download latest version node)
step 06: nvm ls-remote (which make all the version available)
step 07: nvm install 14 (An Example)
step 08: nvm use 14 (this make it as default version)
source: https://github.com/nvm-sh/nvm
Solution 5:[5]
Try upgrading to v17.6.0. It solved the issue for me.
Solution 6:[6]
In My case eslint 8.9.0 was the culprit. Rollback fixed it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pragyan |
| Solution 2 | Rafiq |
| Solution 3 | Owolabi Ezekiel Tobiloba |
| Solution 4 | Praney Pareek |
| Solution 5 | Harijoe |
| Solution 6 | Rohan Sharma |
