eslint issue for properties with underscore as prefix

Viewed 3402

I am beginner in NodeJS and MongoDB, and while learning from some online resources I had created mongoose schema as:

const mongoose = require('mongoose')

const blogSchema = mongoose.Schema({
  title: String,
  author: String,
  url: String,
  likes: Number,
})

blogSchema.set('toJSON', {
  transform: (document, returnedObject) => {
    returnedObject.id = returnedObject._id.toString()
    delete returnedObject._id
    delete returnedObject.__v
  }
})

But eslint is keep giving me errors:

  1. for returnedObject.id, delete returnedObject._id and delete returnedObject.__v - Assignment to property of function parameter 'returnedObject'.eslint(no-param-reassign)
  2. for returnedObject._id and returnedObject.__v - Unexpected dangling '_' in '_id'.eslint(no-underscore-dangle)

snapshot of the eslint errors

What is the correct way in which I can remove the _id and __v fields and re-assign the _id to id for returnedObject?

I am using airbnb-base eslint configuration (devDependencies from package.json):

    "eslint": "^6.8.0",
    "eslint-config-airbnb-base": "^14.0.0",
    "eslint-plugin-import": "^2.20.1",
1 Answers

As suggested by @CherryDT and @slebetman in above comments - there is nothing wrong with the code and the issue is only related to the eslint configuration.

I come up with the following solutions:

Disable for line

Add a comment above the line where you see this error:

// eslint-disable-next-line no-param-reassign, no-underscore-dangle
returnedObject.id = returnedObject._id.toString()

or next to the line:

returnedObject.id = returnedObject._id.toString() // eslint-disable-line no-param-reassign, no-underscore-dangle

Disable for file

Add the comment at the first line in the file:

/* eslint-disable no-param-reassign, no-underscore-dangle */

Turn off the rule in eslint configuation (usually .eslintrc.js, .eslintrc.yml or something similar with name .eslintrc.*)

rules: {
  no-underscore-dangle: off,
  no-param-reassign: off,
}

The example above if for the yml configuration, something similar can be done with other configurations.

The first two solutions are not good fit for a huge code base, so I recommend to use the third option.

*Multiple errors can be turned off in the same comment with comma separated.

Related