小编hel*_*999的帖子

如何在 mongodb 响应中排除密码字段?

每当向后端发出获取用户的请求时,我都会使用散列密码返回该用户。

        {
            "_id": "5e4e3e7eecd9a53c185117d4",
            "username": "rick",
            "email": "rick@gmail.com",
            "password": "$2b$10$/eD8g4jCw6Bx0.FNxFDADO5tc70AvUmK0H/7R/0JTyo2q9PcGAdOO",
            "createdAt": "2020-02-20T08:08:30.878Z",
            "updatedAt": "2020-02-20T08:08:30.878Z",
            "__v": 0
        }
Run Code Online (Sandbox Code Playgroud)

用户模型

const userSchema = new Schema({
    username: { type: String, required: true },
    email: { type: String, required: true },
    password: { type: String, required: true },
    posts: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Post"
    }]
}, { timestamps: true })
Run Code Online (Sandbox Code Playgroud)

列出用户路由

    listUsers: (req, res) => {
        User.find{}, (err, users) => {
            if (err) {
                return res.status(500).json({ error: "Server error occurred" })
            } else if …
Run Code Online (Sandbox Code Playgroud)

mongodb express

1
推荐指数
2
解决办法
5238
查看次数

标签 统计

express ×1

mongodb ×1