如何修复错误(TypeError:无法分配给对象'#<QueryCursor&>'的只读属性'map') [英] How to fix the error (TypeError: Cannot assign to read only property 'map' of object '#<QueryCursor>')

查看:0
本文介绍了如何修复错误(TypeError:无法分配给对象'#<QueryCursor&>'的只读属性'map')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Mongoose将我的数据发送到MongoDB。现在,在为它提取API路由的过程中,抛出了一个错误。

代码

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

错误发生在const res = await fetch("/api/sendChoice" ,{

终端服务器出现错误

Error-TypeError:无法分配给对象的只读属性‘map’ ‘#<;QueryCursor&>’

在检查元素中,错误为

我找不到与解决此问题相关的任何内容,我甚至不明白尝试亲自解决该错误意味着什么。

我的项目中的一些其他相关代码:

接口/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);
    }
}

MongoDB架构

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

推荐答案

17.5.0版的最新更新会导致此错误。您必须将节点js重新安装到版本16.14.0 LTS。您应始终使用LTS版本

这篇关于如何修复错误(TypeError:无法分配给对象&#39;#&lt;QueryCursor&>&#39;的只读属性&#39;map&#39;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆