New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: return a null value for undefined nested keys #4620
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we do need to do this, but let's also keep a console.warn with that error message in dev mode
@KevinVandy added the warning message. |
is it possible for this change to be merged, please? |
I noticed the So it should now handle these cases:
when trying to access |
throw new Error( | ||
`"${key}" in deeply nested key "${accessorKey}" returned undefined.` | ||
) | ||
if (result == null || !(key in result)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be "===" triple equals equality check? and actually, wouldn't we want to check for undefined? Why is the result assignment moved to the last line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used "==" to handle both undefined and null, because you cannot use the in
operator on either of them. Unless you want to handle undefined and null differently?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SeanStove I'm working on a PR where I'm simply making this change. Wonder if it works in your testing
accessorFn = (originalRow: TData) => {
let result = originalRow as Record<string, any>
for (const key of accessorKey.split('.')) {
result = result?.[key]
if (process.env.NODE_ENV !== 'production' && result === undefined) {
console.warn(
`"${key}" in deeply nested key "${accessorKey}" returned undefined.`
)
}
}
return result
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KevinVandy Tested and it covers the 3 cases I described above. Thanks for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shipped this last week if you hadn't seen that yet
} | ||
|
||
result = result[key] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this still be above the if statement?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The result assignment is moved to the last line because you cannot read properties of null/undefined (which is covered by the if statement above it).
This code was good, but cleaned up a little in this another PR. Closing this in favor of #4678 |
Since PR #4407 undefined nested keys throw an error message.
This might be helpful, but does not solve the issue when you have to work with data which looks like
accessor
address.street
in this case will throw the error in dev.(or will break the application later in production in case the data here is not as complete)
So the suggestion from @dennemark in issue #4499 makes sense to me.
This PR will return a
null
value, if the nested value does not exist.Optionally, there could also be a
console.warn
statement with what used to be the error message.Fixes: #4499