简体   繁体   中英

I don't understand the use of the spread operator with mongoose

I don't understand the use of the spread operator in the following code:

exports.create = (req, res, next) => {
    const itemObject = JSON.parse(req.body.item);
    const item = new Item({
        ...itemObject,
        name: 'test' 
    });
};

I tried to write itemObject instead of ...itemObject but it doesn't work. Why do you have to create a copy of the object with spread operator for this to work?

Thanks for your help

Because if you don't spread it you're creating an "itemObject" property and value. You're effectively doing this:

const item = new Item({
  itemObject: itemObject,
  name: 'test'
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM