Node.js Mongoose不保存数组元素
我不熟悉Node和Mongoose,我正在尝试使用MERN堆栈构建一个应用程序 当我使用mongoose将数据从我的状态保存到mongodb时,我试图保存的数组的内容实际上不会被保存 这就是我的测试数据从客户端发送到服务器时的样子: 当我查看Mongodb Atlas时,它看起来是这样的: 那里的阵列似乎是空的 相应的模型如下所示:Node.js Mongoose不保存数组元素,node.js,reactjs,mongodb,mongoose,mongoose-schema,Node.js,Reactjs,Mongodb,Mongoose,Mongoose Schema,我不熟悉Node和Mongoose,我正在尝试使用MERN堆栈构建一个应用程序 当我使用mongoose将数据从我的状态保存到mongodb时,我试图保存的数组的内容实际上不会被保存 这就是我的测试数据从客户端发送到服务器时的样子: 当我查看Mongodb Atlas时,它看起来是这样的: 那里的阵列似乎是空的 相应的模型如下所示: const mongoose = require("mongoose"); const Schema = mongoose.Schema; const Lan
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const LanesSchema = new Schema(
{
lanes: [
{
id: String,
title: String,
tasks: [
{
id: String,
title: String,
status: String,
description: String,
priority: String
}
]
}
]
},
{ minimize: false }
);
const Lane = mongoose.model("Lanes", LanesSchema);
module.exports = Lane;
const express = require("express");
const router = express.Router();
const Lane = require("../models/lanes");
router.post("/save", (req, res) => {
const data = req.body;
const newLane = new Lane(data);
newLane.save(error => {
if (error) {
res.status(500).json({ msg: "Internal server error" });
return;
}
res.json({
msg: "We received your data!"
});
});
});
module.exports = router;
router.post("/save", (req, res) => {
const data = req.body;
const newLane = new Lane();
newLane.collection.insertMany(data, err => {
if (err) {
console.log(err);
} else {
console.log("Multiple docs inserted");
}
});
});
我想这里的模型可能有问题,但我不确定
我也在这里尝试了这个版本,但也不起作用:
const LanesSchema = new Schema([
{
id: String,
title: String,
tasks: [
{
id: String,
title: String,
status: String,
description: String,
priority: String
}
]
}
]);
代码的保存部分基本上如下所示:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const LanesSchema = new Schema(
{
lanes: [
{
id: String,
title: String,
tasks: [
{
id: String,
title: String,
status: String,
description: String,
priority: String
}
]
}
]
},
{ minimize: false }
);
const Lane = mongoose.model("Lanes", LanesSchema);
module.exports = Lane;
const express = require("express");
const router = express.Router();
const Lane = require("../models/lanes");
router.post("/save", (req, res) => {
const data = req.body;
const newLane = new Lane(data);
newLane.save(error => {
if (error) {
res.status(500).json({ msg: "Internal server error" });
return;
}
res.json({
msg: "We received your data!"
});
});
});
module.exports = router;
router.post("/save", (req, res) => {
const data = req.body;
const newLane = new Lane();
newLane.collection.insertMany(data, err => {
if (err) {
console.log(err);
} else {
console.log("Multiple docs inserted");
}
});
});
这是格式为JSON的req.body:
[
{
"id":"1",
"title":"Open",
"tasks":[
{
"id":"1",
"title":"Test task",
"status":"Open",
"description":"Test description",
"priority":"High"
},
{
"id":"4",
"title":"Test task 4",
"status":"Open",
"description":"Test description",
"priority":"Normal"
}
]
},
{
"id":"2",
"title":"In Progress",
"tasks":[
{
"id":"2",
"title":"Test task 2",
"status":"In Progress",
"description":"Test description",
"priority":"Normal"
},
{
"id":"3",
"title":"Test task 3",
"status":"In Progress",
"description":"Test description",
"priority":"Normal"
}
]
},
{
"id":"b0d547b1-f669-418e-8558-4739b15e1ef6",
"title":"testLane",
"tasks":[
]
}
]
现在我不确定问题出在哪里。到目前为止,我还没有找到类似的问题
如果我丢失了可能是问题原因的部分代码,请让我知道
提前感谢。谢谢@sushant mehta的评论
您正在尝试添加多个条目,但正在使用“保存”,请尝试使用insertMany 这帮我完成了任务 我的代码现在如下所示:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const LanesSchema = new Schema(
{
lanes: [
{
id: String,
title: String,
tasks: [
{
id: String,
title: String,
status: String,
description: String,
priority: String
}
]
}
]
},
{ minimize: false }
);
const Lane = mongoose.model("Lanes", LanesSchema);
module.exports = Lane;
const express = require("express");
const router = express.Router();
const Lane = require("../models/lanes");
router.post("/save", (req, res) => {
const data = req.body;
const newLane = new Lane(data);
newLane.save(error => {
if (error) {
res.status(500).json({ msg: "Internal server error" });
return;
}
res.json({
msg: "We received your data!"
});
});
});
module.exports = router;
router.post("/save", (req, res) => {
const data = req.body;
const newLane = new Lane();
newLane.collection.insertMany(data, err => {
if (err) {
console.log(err);
} else {
console.log("Multiple docs inserted");
}
});
});
你能将你的req.body作为json添加到问题中吗?@SuleymanSah我添加了req.body。它必须是JSON吗?最好是JSON格式,这样我们可以将它与模式进行比较。@SuleymanSah好的,我明白了。我又更新了我的帖子。感谢您的帮助。您正在尝试添加多个条目,但正在使用“保存”,请尝试使用insertMany。