How to add value to enum array for record?

Options

User can have multiple roles = user, editor, reviewer etc so role is array []

I want to update user record and add one more role. I get user with existing roles and try to add new one to that array.

I get error. How to fix it?

Answers

  • arturosanz
    arturosanz Member ✭✭
    Options

    Pay attention to the text label on top of user.role. It tells that Xano is treating the value as text. But on the left side role has a label enum[]. You are trying to push a value to a text string not to an array. I suggest you to open the dropdown menu and select CONST → array to change the text label for array while keeping the user.role there. Then you shouldn't have problem pushing the value editor which is an allowed value by the enum field.

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options

    The way edit record works is that it replaces the old value with a new value. So what you want to do is push an item to the list, but what it needs is to be told what the new version of the list is. To solve this, first use "get record" to find out the current value. If the get record returns something called "user_1", then we will work with "user_1.role". So what you want to do in your edit record is set that value to user_1.role with a filter of push and your added value.

    That 2-step process (get + edit) is a frequent requirement for managing the complex data types (object, array, json) when the goal is making a change (adding an item, altering a key, etc). Getting past this hurdle puts you in great position to be a Xano power user!

    We work on xano function stack questions all the time as part of our focus on the "hardest 5%" in daily office hours at State Change.

  • Anna Sho
    Anna Sho Member
    edited September 2023
    Options

    Thank you Arturo & Ray.

    It didnt work for me. just to get user record and add user.role as it is array..

    When i select user.role it turns to "text". If I select → array in dropdown it reverts to empty []. Maybe I do something wrong I dunno.

    But I have found workaround that works. I thought it would be less steps))

  • arturosanz
    arturosanz Member ✭✭
    Options

    @Anna Sho I've tested your case and had the same problem with enum lists. Throws the "is not one of the allowable values" error every time you try to update the enum[] field no matter how you do it.

    One workaround that I tested positively is to change the enum[] field type for a text[] type. You lose the automatic value restriction, but you con go on.

    Maybe @Ray Deck knows how to deal with enum[] fields better.

    This example below works well even with the warning…

  • Ambroise
    Options

    I've struggled with this as well, typing in an array as JSON would convert the value to text somehow, had to use "Push" action, too.

    That was so unstraightforward - I had expected to simply select the allowed enum values, but no.