admin管理员组文章数量:1024603
So, I have a prop that will sometimes be null. I want to either get the value or return null.
I am currently doing this:
getOr(
null,
`shift.id`,
ownProps
);
I am getting the shifts.id from the props, however if there's no shift.id
I'll get an error back. How can I either return shift.id
or null
?
So, I have a prop that will sometimes be null. I want to either get the value or return null.
I am currently doing this:
getOr(
null,
`shift.id`,
ownProps
);
I am getting the shifts.id from the props, however if there's no shift.id
I'll get an error back. How can I either return shift.id
or null
?
- What's the error that you get? From my understanding of how _.getOr works then what you have here should work, so my question is what is the specific error - perhaps it's actually something else that you isn't obvious from the code you have here? – ChrisM Commented Apr 5, 2019 at 11:21
- Well, i'm using this to set a variable in graphQL and graphQL tells me it couldn't find it so yeah... – Chris Commented Apr 5, 2019 at 11:24
2 Answers
Reset to default 3The way _.getOr
works is that it provides a default value (the first argument) in the case that the value referenced by the path (the second argument) cannot be found in the object (the third argument). It's actually the same as _.get
but the argument order is flipped.
An example case is here:
let myObjects = [{
shift: {
id: 1,
example: "text1"
}
},
{
shift: {
id: 2,
example: "text3"
}
},
{
shift: {
example: "text2"
}
}
]
myObjects.forEach((object) => {
let result = _.getOr(null, "shift.id", object)
console.log(result);
})
<script src="https://cdn.jsdelivr/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>
The console output is:
1
2
null
From what you have described though it's not clear to me that is actually what you are trying to do. Remember, get/getOr will return what's referenced by the path (even if it's already null) unless it's not there (as in undefined
), at which point it will return the default value.
So in this case, where there is no shift.id
you are asking _.getOr
to return null. It sounds like you are then getting an error from GraphQL when you try to use it with a null value. This isn't a problem with lodash in this case, but a problem with your data / how you are using GraphQL.
If this isn't the right tool for the job then I can't suggest anything else without seeing more of your code/knowing more about the problem. I would suggest taking another look at your overall problem and then perhaps asking a different question if needs be.
I have tried your snippet with the sparse information you provided and that works as designed, as far as I understand this. See also my repl.it.
code
const _ = require('lodash/fp');
var ownProps = { shifts : { id: 'test' }};
console.log(_.getOr(
null,
`shifts.id`,
ownProps
));
ownProps = { shifts : { foo: 'test' }};
console.log(_.getOr(
null,
`shifts.id`,
ownProps
));
output
test
null
So, I have a prop that will sometimes be null. I want to either get the value or return null.
I am currently doing this:
getOr(
null,
`shift.id`,
ownProps
);
I am getting the shifts.id from the props, however if there's no shift.id
I'll get an error back. How can I either return shift.id
or null
?
So, I have a prop that will sometimes be null. I want to either get the value or return null.
I am currently doing this:
getOr(
null,
`shift.id`,
ownProps
);
I am getting the shifts.id from the props, however if there's no shift.id
I'll get an error back. How can I either return shift.id
or null
?
- What's the error that you get? From my understanding of how _.getOr works then what you have here should work, so my question is what is the specific error - perhaps it's actually something else that you isn't obvious from the code you have here? – ChrisM Commented Apr 5, 2019 at 11:21
- Well, i'm using this to set a variable in graphQL and graphQL tells me it couldn't find it so yeah... – Chris Commented Apr 5, 2019 at 11:24
2 Answers
Reset to default 3The way _.getOr
works is that it provides a default value (the first argument) in the case that the value referenced by the path (the second argument) cannot be found in the object (the third argument). It's actually the same as _.get
but the argument order is flipped.
An example case is here:
let myObjects = [{
shift: {
id: 1,
example: "text1"
}
},
{
shift: {
id: 2,
example: "text3"
}
},
{
shift: {
example: "text2"
}
}
]
myObjects.forEach((object) => {
let result = _.getOr(null, "shift.id", object)
console.log(result);
})
<script src="https://cdn.jsdelivr/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>
The console output is:
1
2
null
From what you have described though it's not clear to me that is actually what you are trying to do. Remember, get/getOr will return what's referenced by the path (even if it's already null) unless it's not there (as in undefined
), at which point it will return the default value.
So in this case, where there is no shift.id
you are asking _.getOr
to return null. It sounds like you are then getting an error from GraphQL when you try to use it with a null value. This isn't a problem with lodash in this case, but a problem with your data / how you are using GraphQL.
If this isn't the right tool for the job then I can't suggest anything else without seeing more of your code/knowing more about the problem. I would suggest taking another look at your overall problem and then perhaps asking a different question if needs be.
I have tried your snippet with the sparse information you provided and that works as designed, as far as I understand this. See also my repl.it.
code
const _ = require('lodash/fp');
var ownProps = { shifts : { id: 'test' }};
console.log(_.getOr(
null,
`shifts.id`,
ownProps
));
ownProps = { shifts : { foo: 'test' }};
console.log(_.getOr(
null,
`shifts.id`,
ownProps
));
output
test
null
本文标签: javascriptLodash getOr for nullStack Overflow
版权声明:本文标题:javascript - Lodash getOr for null - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745507604a2153677.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论