简体   繁体   中英

Assigning object does not work through Rest/Spread Operator

I went to a trouble when doing hardcopy of an object.

let error = { ...err }

So when I wrote that way I received a warning from ESLint "Rest/spread properties are not supported until node.js 8.3.0" and nothing was assigned to my error variable.

I was sure that my node version is much higher than the required one, however I checked - it's ^14.1.0.

ESLint version - ^6.6.0.

Lots of forums said to configure ESLint to make it work, so I added first:

"engines": {
    "node": ">=10.6.0"
}

And then:

"env": {
    "es6": true
},
"parserOptions": {
    "ecmaVersion": 9,
    "sourceType": "module"
}

However, it still doesn't work. err object is defined, when I try to implement in other ways it works, but when assigning through Spread - it doesn't.

Does someone have any idea?

Thanks!

PS when console.log(err.name) I get 'CastError' , when I console.log(error.name) after let error = {...err } , I get undefined.

PPS error inherits some properties from err, but property name is undefined.

PPPS the snippet is

module.exports = (err, req, res, next) => {
    ...

    if(process.env.NODE_ENV === 'production') {
        let error = { ...err }
        if (error.name === 'CastError') {
            error = handleCastErrorDB(error)
        }
        sendErrorProd(error, res)
    }
}

However, this doesn't do what needed. Thus, when I change if statement condition to err.name === 'CastError' instead of error.name === 'CastError' everything works.

Have the same problem on this Udemy course, the solution is:

let error = Object.assign(err)

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