Skip to content
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

Null value in data causes JavaScript error #28

Closed
CoryJ34 opened this issue Jul 22, 2019 · 9 comments
Closed

Null value in data causes JavaScript error #28

CoryJ34 opened this issue Jul 22, 2019 · 9 comments

Comments

@CoryJ34
Copy link
Contributor

CoryJ34 commented Jul 22, 2019

In this codesandbox: https://codesandbox.io/s/adoring-dan-6xw6g

There are two conditions that must be met in order for this error to occur:

  1. There are multiple values referenced in one field (see "testField" in the code sample)
  2. There is an actual null value (as opposed to the field being absent in the data, see "isNull" in the code sample) that occurs before a non-null value

It appears that the issue is related to #26 where only undefined values are checked, but not null. Therefore, the defaultValue is not used and a null value is returned in the reduce function, leading to a "Cannot read property 'replace' of null" error in the next iteration of the reduce function because "str" is null.

@curran
Copy link
Member

curran commented Jul 23, 2019

Nice! Thanks for this find. Would you mind crafting a failing test within the existing test suite and opening a PR that includes the failing test? Thank you.

@CoryJ34
Copy link
Contributor Author

CoryJ34 commented Jul 24, 2019

#29

I added a test case and also included a possible fix for you to look over.

@curran
Copy link
Member

curran commented Jul 25, 2019

Hooray! Thank you for this work. Merged the PR, added a note on this behavoir in the README, and released a new version.

As a side note, I would recommend to set an avatar for your GitHub profile, as then you will appear more professional in your online interactions.

Thanks again for your work here. It's little improvements like this that help really solidify Open Source projects!

@Ashpabb
Copy link

Ashpabb commented Jan 14, 2020

Is this still open?

@curran
Copy link
Member

curran commented Jan 15, 2020

Should have been closed back in July when that PR was merged. Closing now!

@curran curran closed this as completed Jan 15, 2020
@mytharcher
Copy link
Contributor

This error is not fully fixed, which case I have met is:

import parse from 'json-templates';

const template = parse('{{a}} {{b}}');

console.log(template({ a: 0, b: 1 })); // '0 1': right
console.log(template({ a: null, b: null })); // null: emmm, maybe ' ' better, but acceptable

console.log(template({ a: null, b: 1 })); // Error: TypeError: Cannot read properties of null (reading 'replace')

If not provided the default value, and first variable is null, but the second is not, the error occurs.

@curran curran reopened this Apr 30, 2023
curran added a commit that referenced this issue Apr 30, 2023
Fix nullable value error (#28)
@mytharcher
Copy link
Contributor

I found another thing which is not so perfect is the default value will be applied on to null. The case I have met is like this:

expect(template('{{a}}')({ a: null })).toMatchObject({ a: null });
// got { a: undefined } which is not expected

This will cause some logic issue in usage, which null is not strict equal to undefined.

I will send another fix PR if needed.

@curran
Copy link
Member

curran commented Jul 3, 2023

Oh nice! A PR with tests would be most welcome. Thank you.

@curran
Copy link
Member

curran commented Jul 4, 2023

Closed by #45

Feel free to reopen if this comes up again.

@curran curran closed this as completed Jul 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants