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

PersistedModel converting string to number still giving "must be a number" error msg. #4362

Open
heena-agg opened this issue Nov 10, 2023 · 2 comments
Labels

Comments

@heena-agg
Copy link

Steps to reproduce

Current Behavior

Expected Behavior

Link to reproduction sandbox

Additional information

Related Issues

See Reporting Issues for more tips on writing good issues

@heena-agg heena-agg added the bug label Nov 10, 2023
@heena-agg
Copy link
Author

Model repository contains :

modelClass.observe('loaded', async ctx => {
ctx.data.amount = Number(ctx.data.amount)
ctx.data.unappliedAmount = Number(ctx.data.unappliedAmount)
ctx.data.appliedAmount = Number(ctx.data.appliedAmount)
});

still getting error : 
Error: {"error":{"statusCode":422,"name":"UnprocessableEntityError","message":"The request body is invalid. See error object `details` property for more info.","code":"VALIDATION_FAILED","details":[{"path":"/unappliedAmount","code":"type","message":"must be number","info":{"type":"number"}},{"path":"/appliedAmount","code":"type","message":"must be number","info":{"type":"number"}}]}}


Even after applying conversion to Number, still conversion does not happen.
tried parseFloat too. 

@javakamal1
Copy link

@heena-agg

modelClass.observe('loaded', async ctx => { if (ctx.data.amount && !isNaN(ctx.data.amount)) { ctx.data.amount = Number(ctx.data.amount); } if (ctx.data.unappliedAmount && !isNaN(ctx.data.unappliedAmount)) { ctx.data.unappliedAmount = Number(ctx.data.unappliedAmount); } if (ctx.data.appliedAmount && !isNaN(ctx.data.appliedAmount)) { ctx.data.appliedAmount = Number(ctx.data.appliedAmount); } });

This code uses the isNaN() function to check if the fields are numeric before attempting to convert them. If a field is null, undefined, or a non-numeric string, isNaN() will return true, and the field will not be converted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants