bla
bla

Reputation: 1055

How i can replace a value in a query using node/adonis?

Actually my table users have one column called permission that stores values: 1,2 and 3.

I have this query that i return all users and i need to replace the value permissions to: 1 replace to 'Admin' 2 replace to 'Normal User' 3 replace to 'Students'

I try this way:

async index({request}) {
    const page = request.input('page')
    const pageSize = request.input('pageSize')
    const users = await User.query().paginate(page, pageSize)
    for(let i=0; i<users.rows.User;i++){
        console.log(users)
        if(users[i].rows.User.permission === 1){
            users[i].rows.User.permission = 'Admin'
        }
    }
    return users
}

But my for() is not findind the users. How i can change this value? There's a way to do this using the lucid models?

Upvotes: 1

Views: 358

Answers (1)

crbast
crbast

Reputation: 2302

Try to change you loop like :

...

users.rows.forEach((user, index, users) => { // see below
   if(user.permission === 1){
       users.rows[index].permission = 'Admin'
   }
   ...
})

return users

^ Help : is it possible to change values of the array when doing foreach in javascript?

Some information

users[i].rows.User.permission is wrong. Try it :

// Do not use .User
users.rows[i].permission

Upvotes: 1

Related Questions