Mongoose没有在数组中保存我的新文档



所以我的代码是:

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

我想把任务(卡(保存在板对象中。

我的res是:

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

将您的参考字段级联到相反的方向:

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

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

相关内容

  • 没有找到相关文章

最新更新