Getting only the INTof a record id from a get all records query

Options
simon
simon Member
edited October 2023 in ? Help! I'm a Noob

I am unable to get a only the INT from a simple get all records query.

I am getting the company id from the logged in user and store it in buyer_id.
Using the buyer_id, I filter my second query certificates using only the company_id INT.

How can I get only the number the INT and not the object with the number? The closest I get is [2] or [{id: 2}] and not 2

Happy to hear other ways of doing it.

Essentially I simply want to display certificates that belong to a logged in user.
My shema:

table: "users"

table: "companies" links to > user_id

table: "certificates" links to > "companies"

Many thanks.



Best Answer

  • arturosanz
    arturosanz Member ✭✭
    edited October 2023 Answer ✓
    Options

    @simon Query All Records returns a list by default. If you want it to return a single value (the first one found) you must select single from the configure return dropdown menu.

    Another option would be to use the Get Record function instead, which always returns a single value or null.

    Your company_id variable returned from the first Query All Records function will be a list by default, so the Create Variable buyer_id = company_id.id won't work.

    You can access the item from a list by its index like so company_id.0.id where 0 is the item index. You can either use the dot notation or the get filter to access a specific item from a list.

    Another option would be to use the json_encode and json_decode filters to convert arrays and objects to text strings and viceversa, and play with it.

Answers

  • Cameron B
    Cameron B Member, Administrator

    ADMIN

    Options

    Hey there - it looks like you're doing the right thing to get the id of the record. In fact, you are returning only the id of the record.

    The [] brackets are signifying to us that the returned values are in a list. This is the symbol computers use to determine if something is a list so we can continue to use this data within our backend or our frontend.

    From this list, we can turn it into a string if we want to get rid of the [] symbol - but be warned, this typically means the backend and frontend won't recognize the list as a list — instead that list will be seen as a string. Then, to turn it back into a list you can use the split function, where you split each item by a delimiter of choice (typically a comma).

    I hope this makes sense, but let me know if there's anything I'm not getting or where I can help clarify :)

  • simon
    simon Member
    Options

    Thanks Cameron! Glad to hear I am on the right track.
    How would I convert a list into a string?

    When I return the buyer_id I get the id as list (first screenshot, first post).
    However, when I use this result to filter the certificates by buyer_id I get an error.
    ParseError: "certificates.buyer" = [2], message=Lists are not supported.

    When I use a string/text constant to filter the certificates by, I get my desired result.

    How can I filter certificates by buyer_id?


  • Cameron B
    Cameron B Member, Administrator

    ADMIN

    edited October 2023
    Options

    Is your buyer_id a list value? We want to ensure that it's an int, as we're comparing it to the id column, which is an int.

  • simon
    simon Member
    Options

    Hi Cameron and Arturo,

    the configure return menu was what I was looking for! I did not know it exists. It works now. Thanks a ton. A few more questions came up, but that's for a different post.

  • Cameron B
    Cameron B Member, Administrator

    ADMIN

    Options

    Awesome, thanks for following back up and letting us know!!

  • Matt
    Matt Member
    Options

    Where does one find the "configure return dropdown menu"? I'm struggling with a similar issue.

  • Matt
    Matt Member
    Options

    Found the 'single' option, error persists. Do you need any "input" value if the function is to use the authenticated user ID?

  • Matt
    Matt Member
    Options

    I'm stuck.

    I have provider_users, in a reference field for providers.

    The goal is to only return the "providers" where the user ID matches.

    The provider_users_id field on "providers" is an int list since multiple provider_users can be linked to the "provider".

  • Cameron B
    Cameron B Member, Administrator

    ADMIN

    Options

    @Matt Under your query, under the output tab, you can select the little white pencil and change type from list to single :)

  • Matt
    Matt Member
    Options

    @Cameron B Yes, I tried that previously and just tried again. Same error.

  • Cameron B
    Cameron B Member, Administrator

    ADMIN

    Options

    Gotchya!

    It looks like you want to search that column for the user that matches the auth id. To do this, in your query, you actually add a joins. The joins that you add is of this table, of the specific column you want to search.

    Then, in your configure search query you should see the new column broken into its children columns that you can select and search by.

  • Matt
    Matt Member
    Options

    Thanks @Cameron B - that worked!

    I don't quite follow why the Join was necessary here. Do you mind explaining?

  • Cameron B
    Cameron B Member, Administrator

    ADMIN

    Options

    It's a little magic, but more or less the idea is a joins is allowing Xano to look into the column as another searchable table. It's super useful!