简体   繁体   English

如何使用 node/adonis 替换查询中的值?

[英]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.实际上,我的表用户有一列称为permission ,用于存储值:1,2 和 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'我有这个查询,我返回所有用户,我需要将值权限替换为: 1 替换为“管理员” 2 替换为“普通用户” 3 替换为“学生”

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.但我的 for() 没有找到用户。 How i can change this value?我怎样才能改变这个值? There's a way to do this using the lucid models?有没有办法使用清醒模型来做到这一点?

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? ^ 帮助: 在 javascript 中进行 foreach 时是否可以更改数组的值?

Some information一些信息

users[i].rows.User.permission is wrong. users[i].rows.User.permission是错误的。 Try it:试试看:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM