猫鼬不保存数组元素

时间:2020-02-13 08:35:58

标签: node.js reactjs mongodb mongoose mongoose-schema

我是Node和Mongoose的新手,我正在尝试使用MERN堆栈构建应用程序。

当我使用mongoose将数据从状态保存到mongodb时,我要保存的数组内容实际上并没有得到保存。

这是我的测试数据从客户端发送到服务器时的样子: enter image description here

当我检查Mongodb Atlas时,它看起来像这样: enter image description here

那里的数组似乎是空的。

相应的模型如下:

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 LanesSchema = new Schema([
    {
        id: String,
        title: String,
        tasks: [
            {
                id: String,
                title: String,
                status: String,
                description: String,
                priority: String
            }
        ]
    }
]);

代码的保存部分基本上是这样的:

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;

这是格式为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":[

      ]
   }
]

现在我不确定是什么问题。到目前为止,我还找不到类似的问题。

如果我缺少代码的某些部分(可能是造成问题的原因),请告诉我。

谢谢。

1 个答案:

答案 0 :(得分:0)

感谢@sushant mehta进行评论。

“您正在尝试添加多个条目,但正在使用保存,请尝试使用insertMany。”

这为我完成了工作。

我的代码现在看起来像这样:

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