Integer Array behaving like Integer in API call

Options

Forum was very helpful yesterday. Giving it another go :)

CONTEXT: I'm creating a new Post. One post is related to many technologies via an Integer Array field (i.e. list of integers) that links POSTS and TECHNOLOGIES tables.

ISSUE: Xano correctly links TECHNOLOGY when the array has just one integer (ID), but does not accept & link up an array of integers. The error I get is “ERROR_CODE_INPUT_ERROR”

VISUAL BREAKDOWN:

It all works as intended in the debugger, creating a post with 2 technology ids:

It does not work in Production:

I have a custom frontend (React Native) using Axios for network requests…
→ formData.append('technologies_id', technologies.toString());

Where “technologies” is an array of integers.

I also tried this version:
→ formData.append('technologies_id', JSON.stringify(technologies));

For both: Status 400

LOGS:

But it works with just one number inside the technologies array: I changed the frontend code to make "technologies_id" just one integer and it works.
—> formData.append('technologies_id', technologies[0].toString());

And also created a post where “technologies” was an array with a single Technology ID inside, and it worked:

So it seems like an odd type error - Xano accepts "technologies_id" as a single integer & as an array with one integer element inside, but not when it's an array of integers, even though that's how its setup in the backend.

ALSO TRIED: I also tried changing the name of the input variable to “technologies” instead of “technologies_id" to see if it was an issue of crossed wires with naming, but that didn't make a difference.

SUMMARY: can't save array of integers in Xano, because it's expecting a single integer, despite the field type explicitly being a List (array) of integers. Wondering if there's some manual deserializing I'm supposed to do…

I watched this vid but didn't find it directly relevant / helpful (https://youtu.be/lYNwajsknKY)

thank you very, very much in advance!

Best Answers

  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Answer ✓
    Options

    Hi @Jswan — it looks like this was already answered in this post:

    Or is this a different question now?

  • MattLaro
    MattLaro Member ✭✭
    edited March 2023 Answer ✓
    Options

    It seems to be the same question to which I've provided an answer, but here I'll try to explain by parts :

    Why a single integer works?

    The beauty of Xano is that you can pass a single value in an array of integers and it will still "implicitely" convert it to an array of integer.

    Suppose we have this simple function :

    If I do this in run and debug :

    It will interpret it as this in the results :

    If I do this in run and debug :

    Notice here that I passed my integer as text. It will implicitely convert the text as integer, then implicitely put it in an array.

    Why it doesn't work in an array ?

    If you look closely at your output, it suggests that the API received your array as a text value rather than an array, where

    Should have been technologies_id : [5,6] (array of integers)
    Actually received technologies_id : "[5,6]" (text)

    If you try to pass an array inside double quotes as if it was text, it will fail the same way it fails in your API call history.

    You can recreate this easily in debug :

    I guess other API can work around the fact that an array is passed as text easily, but not Xano for that matter.

    Why does it work when I do it in Debug ?

    Because you pass an array in the correct way for Xano to understands.


    Your test :

    Well, what can I do ?

    Here was the suggested workaround :

    1. Change your integer list input for a json input.

    2. Create a variable where you will store your input using a "safe_array" filter.

    The safe_array filter will make sure that

    • whenever you received your array in text or real array format, that it returns an array. "[5,6]" will become [5,6].
    • Whenever you receive empty input, it will give you an empty array. "" becomes []
    • Whenever you receive an input that is not an array, that it will put it in an array. "1" becomes [1]

    So you create let's say, filtered_integer_list out of my_integer_list and you apply a safe_array filter, like this :

    3. Use that variable in your database operations to store the array of numberrs.

    If we then pass "[5,6]" to my_integer_array, it will correctly convert the text to an array of numbers and you can proceed with storing those in your database correctly :

    Let's try again :

    Hope it helps!

Answers

  • Jswan
    Jswan Member
    Options

    Awesome! I think the community forum glitched - promise I didn't ask this again yesterday xD.

    Really appreciate you walking through this & your comment in the other post! 🤙 Makes perfect sense & works beautifully. Cheers!