Timo222
Timo222

Reputation: 67

Mongoose not saving my new document in array

so my code is that:

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

i want to save task (card) inside the board object.

my res is that:

"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

the problem is when i refresh or sending request again the array of cards is empty again. its not saving the last doc ("two") i added. what i am doing worng here?

update - schema of board

this is the 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);

this is the List schema

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

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

this is the Task schema:

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

Upvotes: 0

Views: 61

Answers (1)

Luis Orbaiceta
Luis Orbaiceta

Reputation: 473

Cascade your reference fields in the opposite direction:

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

See if data gets persisted following this logic

Upvotes: 1

Related Questions