是否有人知道如何修复此错误(TypeError:无法分配给对象'#<QueryCursor&>'的只读属性'map') [英] Does anyone know nay fix for this error (TypeError: Cannot assign to read only property 'map' of object '#<QueryCursor>')

查看:44
本文介绍了是否有人知道如何修复此错误(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: Cannot assign to read only property 'map' of object '#<QueryCursor>'

在Inspect元素中,错误为 this

我找不到与解决此问题相关的任何内容,我甚至不明白尝试自己解决此问题是什么意思。

来自我的项目的其他一些相关代码 接口/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天全站免登陆