Dynamic object reference?

Options

hi all, super simple question that I’ve tried to answer on my own, but struggled.

I’ve got an input field (string) called selectProperty. Property could have a value of “name”, “description” or “url”.

I’d like to loop through a ‘get all records’ query, and retrieve item.selectProperty but unsure how I’d convert it to a valid path e.g item.name

Apologies for the very basic question, but I really look forward to your answers.

Comments

  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options

    Hi @ribotmaximus - you should be able to do this all with your Query All Records function. By clicking on the function you should open the Filter section and find the "By Custom Query" section. There, you can define different WHERE statements to say something like

    WHERE db: table.name = input: selectProperty

    OR

    WHERE db: table.description = input: selectProperty

    etc, etc…

    If your desire is to return a single item, you can also change the output type of the function from a list to single.

    Check out more on how to use this powerful function (start at 1:53 for By Custom Query info):

  • ribotmaximus
    ribotmaximus Member
    Options

    Thanks @Michael Udinski .

    My original question wasn’t very clear. I actually need to do something like

    SELECT input:selectProperty FROM db.tableName

    Originally, I was trying to use an eval type of statement to access the Object’s property:

    Eval(“item.” + input:selectProperty)

    An alternative (which I haven’t tried yet, is to use Property Accessors:

    Item[input:selectProperty]

    Are any of the three options above possible within xano?

    Many thanks for the amazing support!

    Antony

  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    edited July 2023
    Options

    Are you just wanting to return a specific field?

    I'm unclear what your end goal is

  • ribotmaximus
    ribotmaximus Member
    Options

    Hi @Michael Udinski - good point. My goal is to be able to supply an optional querystring parameter that instead of returning all properties in the response, only returns the fields that have been supplied via the querystring.

    Say for example, there's a User table with the following fields: id, username, email, password, magic_link, created_at, state

    /users/ would return all the fields above.

    Whereas the call below would only return the Name and Description fields:

    /users/?fieldList=Name,Description

    I've since discovered the get and set filters allow for dynamic referencing of variables! I'm still getting used to using them as they don't feel completely natural to me yet (it's early days).

    But I can now supply a list of fields via the querystring (comma delimited), which I loop each item of the DB query and then loop through the field list retrieving the correct item and value via the set and get. The crux of what I was trying to do can be seen on the right hand panel.

    There's probably some big no nos with what I've created, please do let me know if that's the case.