10

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
this

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);
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Pragyan
  • 349
  • 2
  • 14
  • In the screenshot you provided, the error is a 500 Internal Server Error. If you created the server for which this call is made, have you tried debugging it? – tomerpacific Feb 10 '22 at 22:40
  • @tomerpacific The server works fine i think because the google auth data is getting saved. – Pragyan Feb 10 '22 at 22:43
  • @tomerpacific [issue](https://github.com/Automattic/mongoose/issues/11377) it just happened, thats why my app crashed. if you know any alternative please tell – Pragyan Feb 11 '22 at 00:25
  • https://stackoverflow.com/questions/71079556/does-anyone-know-nay-fix-for-this-error-typeerror-cannot-assign-to-read-only-p – Hemant Vishwakarma Feb 11 '22 at 11:45

7 Answers7

29

the latest update to version 17.5.0 is the one causing this error. You must reinstall node js to version 16.14.0 LTS. You should always work with LTS versions

sebaliux
  • 414
  • 2
  • 2
  • Hey, so how do I keep my version of nodejs when ubuntu wants to automatically update for me, and isn't sticking with the LTS version? – Bennington Feb 18 '22 at 03:44
  • @Bennington You can try using nvm instead of installing Node directly - https://github.com/nvm-sh/nvm – Kamen Minkov Feb 18 '22 at 23:34
9

This issue occured recently and apparently its happening with latest version of node.

issue link

So you can change the version of node to older version and it will be fixed. I am using node version v14.19.0

Pragyan
  • 349
  • 2
  • 14
7

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"]
Rafiq
  • 8,987
  • 4
  • 35
  • 35
1

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

1

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

Praney Pareek
  • 49
  • 1
  • 5
1

Try upgrading to v17.6.0. It solved the issue for me.

Harijoe
  • 1,771
  • 1
  • 16
  • 27
0

In My case eslint 8.9.0 was the culprit. Rollback fixed it.

Rohan Sharma
  • 1,416
  • 2
  • 14
  • 19