简体   繁体   English

我不明白扩展运算符与 mongoose 的使用

[英]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.我试图写itemObject而不是...itemObject但它不起作用。 Why do you have to create a copy of the object with spread operator for this to work?为什么必须使用扩展运算符创建 object 的副本才能使其正常工作?

Thanks for your help谢谢你的帮助

Because if you don't spread it you're creating an "itemObject" property and value.因为如果你不传播它,你正在创建一个“itemObject”属性和值。 You're effectively doing this:您正在有效地执行此操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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