Is this a bug? Api action json data, i cant format correctly the values of an array

Hi, I am working with an api which requires that I send with the POST method an array with 2 number fields: “unit_price” and “quantity”.
I get these values from this query:

So this is my code inside data json calling the query array:

This is the api response:

As you can see it says: “items.0.quantity must be a number”

So the problem is that the json puts the fields as strings:

"unit_price": "54.00",
"quantity": "1"

And it should be of type number:

"unit_price": 54.00
"quantity": 1

Without the quotes in the numbers

I already tried calling the query in the following way:

{{customv.toNumber()}} and {{customv.values()}}

But it does not work.

I tested using:

{
  "items": [
    {
      "quantity": 1,
      "unit_price": 45
    }
  ]

And of corse it works ok
I tryed with a normal Database Query and with a Custom Query as u can see in the images.

I hope you guys can tell me if it is a Wappler bug or there is something I am not seeing.
Thanks in advance.

It probably is already returned from the database as string, I can’t imagine that the api converted them to string. What are the field types in the database and which database and server model do you use?

Thanks for the quick reply Patrick, I am using php as a server model and the fields are:
unit_price (decimal)
quantity (int)

The db is Mysql

and you are right it comes as string from the database but i dont know why:

{
    "customv": [
        {
            "unit_price": "54.00",
            "quantity": "1"
        },
        {
            "unit_price": "48.00",
            "quantity": "1"
        },
        {
            "unit_price": "77.00",
            "quantity": "1"
        },
        {
            "unit_price": "31.00",
            "quantity": "1"
        },
        {
            "unit_price": "72.00",
            "quantity": "1"
        },
        {
            "unit_price": "321.00",
            "quantity": "1"
        },
        {
            "unit_price": "82.00",
            "quantity": "1"
        },
        {
            "unit_price": "246.00",
            "quantity": "1"
        },
        {
            "unit_price": "15.00",
            "quantity": "1"
        }

This is inside the php server file:

"exec": {
    "steps": [
      {
        "name": "customv",
        "module": "dbupdater",
        "action": "custom",
        "options": {
          "connection": "test_data",
          "sql": {
            "query": "SELECT sale.unit_price, sale.quantity FROM sale",
            "params": []
          }
        },
        "output": true,
        "meta": [
          {
            "name": "unit_price",
            "type": "number"
          },
          {
            "name": "quantity",
            "type": "number"
          }
        ],
        "outputType": "array"
      }

Something you can try is changing the Prepare Statements setting, try the different settings and see if it solves it.

I tried with true, false and auto, unfortunately the problem persists :grimacing: :frowning_face:

You should check if your server is using the native mysql driver (mysqlnd).

Yes my server is using that driver as you can see in this capture:

Enable nd_pdo_mysql.

2 Likes

Ok that did the trick with the int field “quantity” but it didnt with the field “unit_price” which is decimal
Should i use another type of field for money?

{
    "customv": [
        {
            "unit_price": "54.00",
            "quantity": 1
        },
        {
            "unit_price": "48.00",
            "quantity": 1
        },
        {
            "unit_price": "77.00",
            "quantity": 1
        },
        {
            "unit_price": "31.00",
            "quantity": 1
        }

There is no decimal type in PHP and float is not as precise as decimal. You could use double as data type or integer and store the value in cents.

https://bugs.php.net/bug.php?id=69974

1 Like

Thank u very much Patrick, u are the best! :dizzy: