如何修复Express中的'无法读取未定义的属性'map'

时间:2019-04-14 08:37:40

标签: node.js express mongoose mongoose-schema

我正在使用Express,并且填充了一些模式: User -> Role -> Permission 。示例:User_1拥有ADMIN角色并具有一些权限:[CREATE_PRODUCT,UPDATE_PRODUCT,DELETE_PRODUCT] 我确实显示了用户的所有权限(包括_id,名称,...),但重点是我只想使用那些名称。 我使用了map()函数,但是它不起作用。

“ express”:“ ^ 4.16.4”,
“猫鼬”:“ ^ 5.4.20”,

routes / userAPI.js:

User
        .find()
        .populate({ path: 'user_role', populate: { path: 'permissions' } })
        .exec((err, users) => {
            if (err) res.status(500).json(err)
            res.status(200).json({
                message: 'sucesss',
                users: users.map(user => {
                    return {
                        user_id: user._id,
                        user_email: user.email,
                        user_password: user.password,
                        user_role: user.user_role.name_role,
                        user_permissions: user.user_role.permissions.map(permission => permission.action_name)
                    }
                })
            })
        })

models / user.js:

email: {...},
  password: {...},
  user_role: {
      type: mongoose.Schema.Types.ObjectId, 
      ref: 'Role',
      default: '5cb2d26f7dd8dd437cdbfe32' //Admin's _id
  }

models / role.js:

name_role: {type: String},
  permissions: [{type: mongoose.Schema.Types.ObjectId, ref: 'Permission'}]

models / permission.js:

action_code : {
    type: String, 
    unique: true
},
action_name : {
    type: String
}

如果不使用map()则输出

[{
    "user_id": "5cb2dcaeca716b2d80665610",
    "user_email": "a@a.com",
    "user_password": "$2b$10$RWGPv88o4FHCQ.72ELtsUe4eEgzZLJc97EfNPeH74N4Mi9b47h/RW",
    "user_role": "Admin",
    "user_permissions": [{
            "_id": "5cb2ca5674fc4a0f3886c7e0",
            "action_code": "1",
            "action_name": "CREATE_PRODUCT",
            "__v": 0
        },
        {
            "_id": "5cb2cb46ce33eb4978f60d21",
            "action_code": "2",
            "action_name": "UPDATE_PRODUCT",
            "__v": 0
        },
        {
            "_id": "5cb2cb51ce33eb4978f60d22",
            "action_code": "3",
            "action_name": "DELETE_PRODUCT",
            "__v": 0
        }
    ]
} 
]  

我期望的输出:

[{
        "user_id": "5cb2dcaeca716b2d80665610",
        "user_email": "a@a.com",
        "user_password": "$2b$10$RWGPv88o4FHCQ.72ELtsUe4eEgzZLJc97EfNPeH74N4Mi9b47h/RW",
        "user_role": "Admin",
        "user_permissions": ["CREATE_PRODUCT, UPDATE_PRODUCT, DELETE_PRODUCT]
    }
]

2 个答案:

答案 0 :(得分:0)

您需要确保每个用户都具有有效角色,并且每个有效角色均具有有效权限。一个用户(数据库中)不具有'role_name'属性,则返回此错误。或检查像这样的空值:

 User
    .find()
    .populate({ path: 'user_role', populate: { path: 'permissions' } })
    .exec((err, users) => {
        if (err) res.status(500).json(err)
        res.status(200).json({
            message: 'sucesss',
            users: users.map(user => {
                return {
                    user_id: user._id,
                    user_email: user.email,
                    user_password: user.password,
                    user_role: user.user_role ? user.user_role.name_role : null,
                    user_permissions: user.user_role ? (user.user_role.permissions ? user.user_role.permissions.map(permission => permission.action_name) : []) : []
                }
            })
        })
    })

答案 1 :(得分:0)

请检查以下代码,使其适合您的条件

User
        .find()
        .populate({ path: 'user_role', populate: { path: 'permissions' } })
        .exec((err, users) => {
            if (err) res.status(500).json(err)
            res.status(200).json({
                message: 'sucesss',
                users: users.map(user => {
                                user.user_permissions = user.user_permissions.map(permission =>  permission.action_name);
                                return user;
                })
            })
        })