最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

MongoDB如何访问关系模型和更新

IT培训 admin 6浏览 0评论

MongoDB如何访问关系模型和更新

当我创建新用户时,我想访问用户的角色名称并进行更新。

如果客户端被重定向为'/ company / register'路由,如果客户端被重定向为'/ user / register'用户角色名称,那么我的用户角色名称将默认为'company',它将是guest或者等等。我是MongoDB的新用户和NodeJS可能我的观点完全错误我愿意接受建议

const UserSchema = new Schema({
    email:{
        type: String,
        required: true,
        default: ""
    },
    encrypted_password:{
        type:String,
        required:true,
        default: ""
    },
    user_role:[
        {
            role_name:{
                type:String
            },
            is_company:{
                type:Boolean,
                default:false
            },
            write:{
                type:Boolean,
                default:false
            },
            read:{
                type:Boolean,
                default:false
            },
            publish:{
                type:Boolean,
                default:false
            }
        }
    ],
    company:[
        {
            company_name:{
                type:String
            },
            address:{
                type:String
            }
        }
    ],


});

我的注册路线

router.post('/register', (req, res) => {
    User.findOne({email: req.body.email})
        .then(company => {
            if (company) {
                return res.status(400).json({
                    email: 'Email already exists'
                });
            }
            else {
                const newUser = new User({
                    // I want to access here
                    email: req.body.email,
                    encrypted_password: req.body.encrypted_password,
                    updated_at: Date.now()
                });
bcrypt.genSalt(10, (err, salt) =>{bcrypt.hash(newUser.encrypted_password, salt, (err, hash) => {newUser.encrypted_password = hash;
                        // New user added
                        newUser
                            .save()
                            .then(user => res.json(user))
                            .catch(err => console.log(err));
                    })
                })
            }
        });
});

我已经尝试过这个,当然,它不起作用

const newUser = new User({
                    email: req.body.email,
                    encrypted_password: req.body.encrypted_password,
                    user_role.role_name: "company",
                    updated_at: Date.now()
                });
回答如下:

我用这种方法解决了我的问题

const company_user_role = {role_name:"company",is_company:true};
                const newUser = new User({
                    email: req.body.email,
                    encrypted_password: req.body.encrypted_password,
                    user_role: company_user_role,
                    updated_at: Date.now()
                });

MongoDB如何访问关系模型和更新

当我创建新用户时,我想访问用户的角色名称并进行更新。

如果客户端被重定向为'/ company / register'路由,如果客户端被重定向为'/ user / register'用户角色名称,那么我的用户角色名称将默认为'company',它将是guest或者等等。我是MongoDB的新用户和NodeJS可能我的观点完全错误我愿意接受建议

const UserSchema = new Schema({
    email:{
        type: String,
        required: true,
        default: ""
    },
    encrypted_password:{
        type:String,
        required:true,
        default: ""
    },
    user_role:[
        {
            role_name:{
                type:String
            },
            is_company:{
                type:Boolean,
                default:false
            },
            write:{
                type:Boolean,
                default:false
            },
            read:{
                type:Boolean,
                default:false
            },
            publish:{
                type:Boolean,
                default:false
            }
        }
    ],
    company:[
        {
            company_name:{
                type:String
            },
            address:{
                type:String
            }
        }
    ],


});

我的注册路线

router.post('/register', (req, res) => {
    User.findOne({email: req.body.email})
        .then(company => {
            if (company) {
                return res.status(400).json({
                    email: 'Email already exists'
                });
            }
            else {
                const newUser = new User({
                    // I want to access here
                    email: req.body.email,
                    encrypted_password: req.body.encrypted_password,
                    updated_at: Date.now()
                });
bcrypt.genSalt(10, (err, salt) =>{bcrypt.hash(newUser.encrypted_password, salt, (err, hash) => {newUser.encrypted_password = hash;
                        // New user added
                        newUser
                            .save()
                            .then(user => res.json(user))
                            .catch(err => console.log(err));
                    })
                })
            }
        });
});

我已经尝试过这个,当然,它不起作用

const newUser = new User({
                    email: req.body.email,
                    encrypted_password: req.body.encrypted_password,
                    user_role.role_name: "company",
                    updated_at: Date.now()
                });
回答如下:

我用这种方法解决了我的问题

const company_user_role = {role_name:"company",is_company:true};
                const newUser = new User({
                    email: req.body.email,
                    encrypted_password: req.body.encrypted_password,
                    user_role: company_user_role,
                    updated_at: Date.now()
                });

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论