【问题标题】:Mongoose not saving my new document in array猫鼬没有将我的新文档保存在数组中
【发布时间】:2020-09-17 07:57:06
【问题描述】:

所以我的代码是:

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

【问题讨论】:

  • 如果您的卡片是您在 boardMembers 中引用的单独文档,您也应该保存它,否则该数据不会保留在您的数据库中
  • 如果我保存卡片,它将添加新的卡片集合,但不会保存到 Board 模型中的 board 对象中:boardLists: Array
  • 您能提供您正在使用的架构吗?
  • 我使用 Board 架构编辑了问题
  • 为了提供一个好的答案,我需要有 List 架构和 Task 架构...

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


【解决方案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);

看看数据是否按照这个逻辑被持久化

【讨论】:

  • 但现在它没有添加到 Board.boardlists.(一些列表).cards。 boardlists 是 ID 数组......不像我之前说的那样,为了在客户端获取数据,我需要通过 id 调用列表并通过 id 调用任务?
  • 我假设您要遵循的逻辑是创建一个新板,然后添加一个新板列表或一组包含在一组新任务中的板列表。每个都有自己的架构,因此,您应该像我向您展示的那样引用它们。如果您想向董事会列表添加新任务,只需找到该列表并添加新任务,因为它的引用应该全部更新...
  • 是的,但是在 board 对象中使用此逻辑,boardlists 数组仅包含 ID,而不像以前的对象。这是一个好方法吗?
  • 是的,当你获取它时,你可以填充 refs 数组,看看这个mongoosejs.com/docs/populate.html
猜你喜欢
  • 1970-01-01
  • 2014-01-29
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
  • 2016-07-30
  • 2014-09-06
  • 1970-01-01
相关资源
最近更新 更多