Home » Javascript » javascript – Adding new object to object. Schema type is Map

javascript – Adding new object to object. Schema type is Map

Posted by: admin February 24, 2020 Leave a comment

Questions:

I’m struggling to update my object which is type:Map,
What I am trying to do is to add new object to object, However I can not find a way to deal with it. Here is what I did, and it doesnt work.

  const newColumn = new Column({
  title,
  tasks: [],
  taskOrder: []
});
const newId = newColumn._id;
// console.log(newColumn);

let Board = await DashBoard.findOne({ _id: dashBoardId });
let ort = Board.columns;

let test = {
  ort,
  [newId]: newColumn
};

const dashBoard = DashBoard.findOneAndUpdate(
  { _id: dashBoardId },
  { $set: { columns: { test } } },
  (err, data) => {
    if (err) console.log(err);
    res.send(data);
  }
);
How to&Answers:

Is columns type is a map?

I think you are missing three points (spread operator):

let ort = Board.columns;

let test = {
  ...ort,
  [newId]: newColumn
};

When three dots (…) occurs in a function call or alike, it’s called a “spread operator” and expands an array or object into a list.

Read more:
https://dev.to/sagar/three-dots—in-javascript-26ci