How to fix the error (TypeError: Cannot assign to read only property 'map' of object '#<QueryCursor>')
How to fix the error (TypeError: Cannot assign to read only property 'map' of object '#<QueryCursor>')
我正在通过 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>'
检查元素中的错误是
我找不到任何与修复此问题相关的内容,我什至不明白错误是什么意思尝试自己解决。
我项目中的其他一些相关代码:
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);
}
}
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);
这个问题是最近发生的,显然它发生在最新版本的节点上。
因此您可以将节点的版本更改为旧版本,它会得到修复。我正在使用节点版本 v14.19.0
版本 17.5.0 的最新更新是导致此错误的原因。您必须将 node js 重新安装到版本 16.14.0 LTS。您应该始终使用 LTS 版本
如果你用的是Docker那么给个版本就可以解决问题
之前:
FROM node:alpine
现在
FROM node:16.5.0-alpine
WORKDIR /app
COPY package.json .
RUN npm install --only=prod
COPY . .
CMD ["npm", "start"]
在我的案例中,eslint 8.9.0 是罪魁祸首。回滚修复了它。
Node.JS 的最新版本是导致此问题的原因。在您的 package.json 中,确保将您的引擎设置为 "engines": { "node": ">=0.12 < 17.5.0" }
,您应该没问题。
此外,如果您使用 docker 进行部署,请确保将 docker 文件中的版本号更改为小于 17.5.0
对我来说效果很好的解决方案:)
第 1 步:打开终端并复制粘贴到命令下方。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | zsh
耐心等待完成。
步骤02:sudo vim ./zshrc
步骤03:按I进入插入模式并复制粘贴到命令下方。三行必须相同
导出NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && 。 "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && 。 "$NVM_DIR/bash_completion"
然后按 ESC 键 :wq (write and quite).
步骤04:brew安装nvm
步骤 05:nvm 安装节点(将下载最新版本的节点)
步骤 06:nvm ls-remote(使所有版本可用)
步骤 07:nvm install 14(示例)
步骤 08:nvm 使用 14(这使其成为默认版本)
尝试升级到 v17.6.0。它解决了我的问题。
我正在通过 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>'
检查元素中的错误是
我找不到任何与修复此问题相关的内容,我什至不明白错误是什么意思尝试自己解决。
我项目中的其他一些相关代码:
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);
}
}
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);
这个问题是最近发生的,显然它发生在最新版本的节点上。
因此您可以将节点的版本更改为旧版本,它会得到修复。我正在使用节点版本 v14.19.0
版本 17.5.0 的最新更新是导致此错误的原因。您必须将 node js 重新安装到版本 16.14.0 LTS。您应该始终使用 LTS 版本
如果你用的是Docker那么给个版本就可以解决问题
之前:
FROM node:alpine
现在
FROM node:16.5.0-alpine
WORKDIR /app
COPY package.json .
RUN npm install --only=prod
COPY . .
CMD ["npm", "start"]
在我的案例中,eslint 8.9.0 是罪魁祸首。回滚修复了它。
Node.JS 的最新版本是导致此问题的原因。在您的 package.json 中,确保将您的引擎设置为 "engines": { "node": ">=0.12 < 17.5.0" }
,您应该没问题。
此外,如果您使用 docker 进行部署,请确保将 docker 文件中的版本号更改为小于 17.5.0
对我来说效果很好的解决方案:)
第 1 步:打开终端并复制粘贴到命令下方。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | zsh
耐心等待完成。
步骤02:sudo vim ./zshrc
步骤03:按I进入插入模式并复制粘贴到命令下方。三行必须相同
导出NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && 。 "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && 。 "$NVM_DIR/bash_completion"
然后按 ESC 键 :wq (write and quite).
步骤04:brew安装nvm
步骤 05:nvm 安装节点(将下载最新版本的节点)
步骤 06:nvm ls-remote(使所有版本可用)
步骤 07:nvm install 14(示例)
步骤 08:nvm 使用 14(这使其成为默认版本)
尝试升级到 v17.6.0。它解决了我的问题。