question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"in" operator errors out if array value data is null or undefined; Should return false

See original GitHub issue

Given the following rule:

{ "in": [ "1", { "var": "MyData.Values" } ] }

Using it on the following data will cause an error when, IMO, it should return false;

{
   "MyData": {
      "Values": null
   }
}

Should be easy enough to check for a null/undefined data set before testing the “in” operation and return false if necessary.

This may also be a problem with other supported array operations. Although, I have not tested them to see.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jwadhamscommented, Oct 18, 2017

I can work on this, you’re right it’s going to make the library more robust.

In the meantime you might be able to make your rules more robust by using an empty array as your default in your var calls, like

{ "in": [ 
  "1", 
  { "var": ["MyData.Values", [] ] } 
] }
0reactions
MaxNodlandcommented, Oct 25, 2017

Came to report this bug. I fixed locally by adding a “safeIn” function

jsonLogic.add_operation('safeIn', (a, b) => { if(!b || typeof b.indexOf === "undefined") return false; return (b.indexOf(a) !== -1); } )

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: cannot use 'in' operator to search for 'x' in 'y'
The in operator can only be used to check if a property is in an object. You can't search in strings, or in...
Read more >
How can I determine if a variable is 'undefined' or 'null'?
Usually you'd have EmpName(or some other variable) be passed into a function, or the return value of another function and therefore declared(Example: "var...
Read more >
7 Tips to Handle undefined in JavaScript - Dmitri Pavlutin
A detailed article about 'undefined' keyword in JavaScript. 7 tips on how to handle correctly 'undefined' and increase code durability.
Read more >
JavaScript Common Mistakes - W3Schools
To solve this problem, you must test if an object is not null , and not undefined . But this can still throw...
Read more >
How to Check if Object is Empty in JavaScript - Samantha Ming
Looks good so far, it returns false for non-objects. ... But watch out! These values will throw an error. // TypeError: Cannot covert...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found