简体   繁体   中英

Check whether mongoose model has a null property

I have a mongoose schema and a model as follows-

const userSchema= new mongoose.Schema({
    email:String,
    password:String,
    username:String,
    language:String,
    paired:Boolean
});

const Pair = mongoose.model("User",userSchema);

While registering a user I take in only the email and password fields as input. At a later point in time,I want the user to fill in the username and language fields but once registered the user only has a email and language field in their record. How to go about this?

Thanks in advance.

Found the answer.

While adding the secondary details,

User.findById(req.user.id,function(err,foundUser){
        if(err){
            console.log(err);
        }
        else{
            if(foundUser){
                foundUser.username = username;
                foundUser.language = language;
                foundUser.save();
            }
        }
    })

This bit of code browses through the database and finds the user who has an id corresponding to the currently logged in users id and updates the fields according to the values provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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