0

What is the better to fix this 'error' of 'is already defined'?

function findOneAndUpdate (find) {
    var find = find || {}
     ...
}

The test from standardjs:

$ standard

... 'find' is already defined.

Any suggestions?

Run
  • 54,938
  • 169
  • 450
  • 748

3 Answers3

1

You need to leave out the var, because the variable find is already declared through the method header:

function findOneAndUpdate (find) {
    find = find || {};
    ...
}
Peter B
  • 22,460
  • 5
  • 32
  • 69
1

ES6+ solution with default parameters:

function findOneAndUpdate(find={}) { ... }

ES5- would be renaming:

function findOneAndUpdate (_find) { 
  var find = _find || {} 
  ... 
}

Otherwise, you can completely replace the variable, like other answers suggest:

function findOneAndUpdate (find) { 
  find = find || {} 
  ... 
}
GMaiolo
  • 4,207
  • 1
  • 21
  • 37
0

Why not just change either the function parameter name or the new variable's name? The variable name find is clashing with the parameter find.

If all you want is give it an empty object as default parameter, you can safely reuse the parameter:

find = find || {}, just remove the var.

Yong Li
  • 607
  • 3
  • 15