Home > Mobile >  Does anyone know nay fix for this error (TypeError: Cannot assign to read only property 'map�
Does anyone know nay fix for this error (TypeError: Cannot assign to read only property 'map�

Time:02-11

I am sending my data to mongodb via mongoose now during the fetch of api route for it, this 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);
        }
    };

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 codes 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);

CodePudding user response:

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

CodePudding user response:

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

  • Related