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?

Share Improve this question edited Apr 5, 2019 at 11:45 ChrisM 1,70315 silver badges25 bronze badges asked Apr 5, 2019 at 11:14 ChrisChris 1554 silver badges12 bronze badges 2
  • 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
Add a ment  | 

2 Answers 2

Reset to default 3

The 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?

Share Improve this question edited Apr 5, 2019 at 11:45 ChrisM 1,70315 silver badges25 bronze badges asked Apr 5, 2019 at 11:14 ChrisChris 1554 silver badges12 bronze badges 2
  • 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
Add a ment  | 

2 Answers 2

Reset to default 3

The 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