Show all records if URL param is empty?

Options

Hi Everyone

I'm doing a fetch GET request to my API endpoint.

I have an external filter setup for members location, which is sent to the API as a URL param.
"URL_ENDPOINT?location=27" and this works. Shows results for location 27.

I have set this up following the Xano docs on External Filtering. My input "location" is of type integer (the id from the locations table).

But how can I return all locations if a user selects nothing in the frontend?
This should be the default value too on page load.

I've tried null or nothing and they don't work.

"?location=null" = ("Value \"null\" is not a valid integer.")
"?location=" [] (empty array)

TIA
~James

Comments

  • Geoffroy-CYD
    Options

    You need to create a variable at the start of your function stack with a condition:

    • If input location is "null" or isEmpty, then var location = null
    • else var location = input location

    Then, in query all, check the "ignore empty values" box and use the variable location instead of input location

  • James Cowen
    Options

    Hey @Geoffroy-CYD

    Thanks for the reply and suggestion. I've followed your suggestion and no matter what I try I keep getting the error:

    {
    code: ERROR_CODE_INPUT_ERROR,
    message: Value "null" is not a valid integer.,
    param
    : location
    }

    This is my function stack (see below screenshot)

    I run and debug the following as I'm trying to test the string of null
    { "location": "null"} - error
    { "location": ""} - empty string works and returns all locations

    Just cannot convert "null" into null.
    Am I missing something?



  • Geoffroy-CYD
    Options

    @James Cowen You have to allow null value for your input and/or delete input in url ({location})

  • Geoffroy-CYD
    Options

    you have to use is_empty filter instead of location == NA in your condition

  • James Cowen
    Options

    @Geoffroy-CYD sorry I don't understand, can you show a screengrab perhaps?

    Do you mean make the location input nullable set to yes?
    is_empty filter makes the type a bool but it still wants an integer?

    If you could show me how to set this up in pictures or video I'd really appreciate it.

    cheers

  • Geoffroy-CYD
    Options

    @James Cowen
    You can't use location input as url param if you want allow null value

    "Do you mean make the location input nullable set to yes?"
    Exactly !

    About is_empty filter :

  • James Cowen
    Options

    @Geoffroy-CYD very confusing but I think I got it working with some changes to my frontend JS.

    thanks kindly for your help.