I want to register users to my mongoDB database using passport-local-mongoose node package, and I have no problem with that, but what I want is to add more than the username and password to the database: for example ( their first and last names, their roles and pretty much anything neccesssary for the user)
how can I do that with passport-local-mongoose, or is there any other way than with that package, I already tried to add the other objects to the database but for some reason it didn't work. here's what I tried to get the extra objects to the database:
app.post('/register', function(req, res){
User.register({username: req.body.username}, req.body.password, function(err, user) {
if (err) {
console.log(err);
res.redirect('/register')
} else{
passport.authenticate('local')(req, res, function(){
User.updateOne({ username: req.body.username }, { $set: { firstName: 'firstnName', lastName: 'lastName' } })
res.redirect('/secrets')
})
};
});
})
and as you can see I used mongoose's updateOne function to set the new values manually but for some reason I don't see them in the database. and also I have contained the first and last names in the User model schema here's the schema:
const userSchema = new mongoose.Schema ({
email: String,
password: String,
firstName: String,
lastName: String
})
any suggestions?
I found the answer, you have to specify the other values of the schema with the username field object:
app.post('/register', function(req, res){
User.register({username: req.body.username, firstName: req.body.firstName, lastName:
req.body.lastName}, req.body.password, function(err, user) {
if (err) {
console.log(err);
res.redirect('/register')
} else{
passport.authenticate('local')(req, res, function(){
res.redirect('/secrets')
};
});
})
you should change the schema to this:
const userSchema = new mongoose.Schema ({
username: String,
password: String,
firstName: String,
lastName: String
})