API Action won't do math with values

Wappler Version : 4.0.3
Operating System : Windows 10 x64
Server Model: NodeJS
Database Type: PostgreSQL
Hosting Type: Docker

Expected behavior

Server action should add 1 to the test value

What actually happens?

Server action concatenates value

How to reproduce

  1. Create server action
  2. Add Set Value step with value 1
  3. Add another Set Value step with Data Binding - Value from step 2 + 1

What happens after that:

  1. Server connect incorrectly add + 1
  2. Output will be 11 instead of 2

I believe this is correlates with Checking for and updating existing records on post

Most likely itā€™s not related to math operation itself, but to how Data Binding saves values, because the only way I could achieve adding +1 is by editing this API action in Code View and correcting expression.

In additional to that: When I have numeric value in Post and trying to add + 1 to this value in Database action - itā€™s concatenates instead of addition.

I think this problem has been around for a while - surprisingly. An alternative to editing the code manually is to create a variable with a value of 1 and use that in the expression. I think this works correctly. Iā€™m sometimes concerned that manual fixes, made by editing the code, might be overwritten when saving the file.

When Iā€™m trying to do some manipulation with value that comes in POST in Database Query - it becomes even weirder - it creates additional post value.

This looks like a bug - but a different issue. I donā€™t suppose this one would cause any problem and you can of course delete the unwanted POST variables.

Can you please just keep different issues in different topics?

Hey there @Petruchocho, I had this yesterday!

The solution is to be sure the two items you are adding are considered to be numbers. Values read from the database and post variables come through as text and need to be converted with .toNumber() before you can do the maths. When one is still a text type then you get concatenation!

And yes, I find sometimes you need to create a variable with Set Value called ā€œoneā€ which is of type number and has been assigned the value of 1 to make things workā€¦

You make a good point about checking that the values are considered numbers, and this will certainly be the solution in many cases. However, I donā€™t think itā€™s a solution in this case; as you mention, itā€™s sometimes necessary to create a ā€˜oneā€™ variable - but this is only to get round a (rather serious) bug.

The problem seems to be that if you enter a simple expression (1), it will not be saved as entered. Itā€™s not very clear from the value as displayed (2), but the problem can be confirmed by clicking the pencil icon for example (3). The result in the browser (4) show the concatenated result. It also shows the variable in the expression is a number (blue, no quotes) and the result is a string (green, in quotes).

image

@TomDā€¦ Yes!

It stems from the complication that + is both addition and concatenationā€¦ and that Wappler sees both 1 and ā€˜1ā€™ as text, which therefore should be concatenated!

Yes, but why then weā€™re setting value type?
Itā€™s number so why heā€™s concatenating?

Because obviously itā€™s a bug and thatā€™s going to be fixed.

2 Likes

This has been fixed in Wappler 4.0.6

1 Like

This topic was automatically closed after 47 hours. New replies are no longer allowed.