猫鼬不将我的新文档保存在数组中

时间:2020-09-17 07:57:06

标签: node.js mongodb express mongoose react-redux

所以我的代码是:

router.put("/add-task/:id", auth, boardAuth, async (req, res) => {
  const listId = req.params.id;

  try {
    const board = await Board.findOne({ _id: req.board._id });
    if (!board) return res.status(404).send("no such board");

    const list = await List.findOne({ _id: listId });
    if (!list) return res.status(404).send("List not found");

    const task = new Task({
      text: req.body.text,
    });

    list.cards.push(task);

    board.boardLists.forEach((list) => {
      if (listId.toString() === list._id.toString()) {
        list.cards.push(task);
      } else {
        console.log('no task');
      }
    });
    await board.save();
    await list.save();
    res.send(board);
  } catch (err) {
    console.log(err);
  }
});

我想将任务(卡片)保存在板对象中。

我的资源是:

"boardMembers": [
        "5f326c2e8b906103642af93b"
    ],
    "boardLists": [
        {
            "cards": [
                {
                    "_id": "5f63147c14ba4d4464e263ea",
                    "text": "two"
                }
            ],
            "_id": "5f622ca82e6edf1eb8dab7b7",
            "title": "list number one",
            "__v": 0
        }
    ],
    "_id": "5f622c702e6edf1eb8dab7b6",
    "boardName": "board one",
    "boardPassword": "123456",
    "boardCreator": "5f326c2e8b906103642af93b",
    "g_createdAt": "2020-09-16T15:17:04.012Z",
    "__v": 2

问题是当我刷新或再次发送请求时,卡阵列再次为空。它不保存我添加的最后一个文档(“两个”)。我在这里干什么?

更新-董事会架构

这是Boardschema

const boardSchema = new mongoose.Schema({
  boardName: {
    type: String,
    required: true,
    maxLength: 255,
    minLength: 2,
  },
  boardPassword: {
    type: String,
    required: true,
    minlength: 6,
    maxlength: 255,
  },
  g_createdAt: { type: Date, default: Date.now },
  boardCreator: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
  boardMembers: Array,
  boardLists: Array,
});

const Board = mongoose.model("Board", boardSchema);

这是列表架构

const listSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true,
    maxLength: 255,
    minLength: 1,
  },
  cards: Array,
});

const List = mongoose.model("List", listSchema);

这是任务模式:

const taskSchema = new mongoose.Schema({
  text: {
    type: String,
    required: true,
    maxLength: 1024,
    minLength: 2,
  },
 taskOf: { type: mongoose.Schema.Types.ObjectId, ref: "List" },
});

1 个答案:

答案 0 :(得分:1)

以相反的方向层叠参考字段:

const boardSchema = new mongoose.Schema({
    boardName: {
        type: String,
        required: true,
        maxLength: 255,
        minLength: 2,
    },
    boardPassword: {
        type: String,
        required: true,
        minlength: 6,
        maxlength: 255,
    },
    g_createdAt: { type: Date, default: Date.now },
    boardCreator: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
    boardMembers: Array,
    boardLists: [{ type: mongoose.Schema.Types.ObjectId, ref: "lists" }],
});

const listSchema = new mongoose.Schema({
    title: {
        type: String,
        required: true,
        maxLength: 255,
        minLength: 1,
    },
    cards: [{ type: mongoose.Schema.Types.ObjectId, ref: "tasks" }],
});

const taskSchema = new mongoose.Schema({
    text: {
        type: String,
        required: true,
        maxLength: 1024,
        minLength: 2,
    },
});

const List = mongoose.model("lists", listSchema);
const Board = mongoose.model("boards", boardSchema);
const Task = mongoose.model("tasks", boardSchema);

按照此逻辑查看数据是否持久

相关问题