最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why use _.get(err, ['data', 'error', 'code']) if you could simply d

programmeradmin2浏览0评论

So I've been looking for an answer given to me in a nodeJS test. Why use _.get(err, ['data', 'error', 'code']) if you could simply do err.data.error.code? And can you think of a better name for err here? after 3 days of searching i can't figure out an answer ,I may need some help Code:

if (_.includes(errorCodesOnWhichToRemoveSynchronization,
    _.get(err, ['data', 'error', 'code']))) {
  // ...
} 

So I've been looking for an answer given to me in a nodeJS test. Why use _.get(err, ['data', 'error', 'code']) if you could simply do err.data.error.code? And can you think of a better name for err here? after 3 days of searching i can't figure out an answer ,I may need some help Code:

if (_.includes(errorCodesOnWhichToRemoveSynchronization,
    _.get(err, ['data', 'error', 'code']))) {
  // ...
} 
Share Improve this question edited Aug 31, 2018 at 10:25 31piy 23.9k6 gold badges51 silver badges68 bronze badges asked Aug 31, 2018 at 9:46 Wael GolliWael Golli 1072 silver badges9 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 6

Update

A preferred way to do this without any external library is to leverage optional chaining.

err?.data?.error?.code

Lodash's _.get is failsafe. If any of the intermediate property doesn't exist, it simply returns undefined, instead of throwing an error.

See the demo below:

var obj = {
  data: {
    
  }
};

try {
  console.log(obj.data.error.code);
} catch(e) {
  console.log('regular method throws error');
}

console.log('lodash returns', _.get(obj, ['data', 'error', 'code']));
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

By the way, in your case, ['data', 'error', 'code'] can simply be data.error.code.

Imagine err.data is undefined or an empty object. In that case, you would get an exception that js cannot read error from undefined. Using underscore/lodash's get function is a "safer" way to access it. It does not throw that exception and just returns undefined.

Optional Chaining (https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining)

err?.data?.error?.code

Lodash has few methods to access properties that are quite handy and _.get is one of them where the main idea is for you to not have to worry if the path is not valid due to a falsy value along the way. Not only that but it has a defaultValue 3rd parameter (different in lodash/fp) where you could set a default value if the path asked for is invalid:

_.get(object, 'a.b.c', 'foo');  // if a.b.c is falsy you would get 'foo'

_.has is another one which would return a boolean if the requested path exists or not.

_.result is super handy when it es to getting a value from a path which goes trough functions. Think about knockout and observables for example where to get from a to b you would usually need to do a().b().c().myValue ... so with _.result you would do:

_.result(obj, 'a.b.c.myValue')

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论