我現在在為其獲取 api 路由期間通過 mongoose 將我的資料發送到 mongodb,這個錯誤是拋出的代碼
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);
uj5u.com熱心網友回復:
版本 17.5.0 的最新更新是導致此錯誤的原因。您必須將 node js 重新安裝到版本 16.14.0 LTS。您應該始終使用 LTS 版本
uj5u.com熱心網友回復:
這個問題最近發生了,顯然它發生在最新版本的節點上。
問題鏈接
因此,您可以將節點的版本更改為舊版本,它將被修復。我正在使用節點版本 v14.19.0
uj5u.com熱心網友回復:
如果您使用的是 Docker,那么提供一個版本將解決問題。
前:
FROM node:alpine
現在
FROM node:16.5.0-alpine
WORKDIR /app
COPY package.json .
RUN npm install --only=prod
COPY . .
CMD ["npm", "start"]
uj5u.com熱心網友回復:
就我而言,eslint 8.9.0 是罪魁禍首。回滾修復了它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/424009.html
上一篇:什么在打字稿中回傳這個函式?
