In my last commit, I have my POST route working perfectly, but after starting implementing PassportJS I'm not able to save new users anymore. I have been deleting the db and the collections in a couple of occasions mainly because I've been changing the structure of the schema, so I want to ask if any kind of corruption is possible, knowing that I always have been manipulating Mongo through MongoDB Compass.
//DB setup
mongoose.set('useFindAndModify', true);
this.connection = mongoose.createConnection(process.env.MONGODB_URL || MONGO_URI, {
useNewUrlParser: true,
useCreateIndex: true
});
this.connection.once('open', args => console.log('DB is connected'));
this.connection.once('error', err => console.log(err));
This is the method executed by the route and the route itself.
private async postUser(req: Request, res: Response) {
const {username, password, email} = req.body;
console.log(username + " " + password + " " + email);
const {salt, hash} = this.genPass(password);
console.log(salt + " " + hash);
const newUser = new User({ username: username, password: hash, salt: salt, email: email });
console.log(newUser);
await newUser.save((err, newUser) => {
console.log('smth');
if (err) {
res.json('username or email already exist');
return console.error(err);
}
else {
console.log(newUser.get('username') + ' created');
res.json({data: newUser});
}
});
}
this.router.post('/sign', (req: Request, res:Response) => {
this.postUser(req, res)
.then(x => console.log (x));
});
The schema
const userSchema = new Schema ({
username: { type: String, required: true, unique: true},
password: { type: String, required: true},
salt: {type: String, required: true},
email: { type: String, required: true, unique:true, lowercase: true}
},
{
timestamps: true,
});
export default model('user', userSchema);
Console output
(node:19004) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and
will be removed in a future version. To use the new Server Discover
and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Server on port 3000
DB is connected
admin passw email
cf0bcf9bd942010fba7bfd1bcde96ae93b2e1a55f078a70b1cb48f000551c4e1
e73a50fb837a55abd9bcc5da1e635cb0c825663aaaf2b3318f8f9bc38ea89e50af2c70f4ee247256a282709b9a6abee0d9ba5
a574d6dec19d519692683027135
{
_id: 5ef6b7111668ca4a3c0e8332,
username: 'admin',
password:
'e73a50fb837a55abd9bcc5da1e635cb0c825663aaaf2b3318f8f9bc38ea89e50af2c
70f4ee247256a282709b9a6abee0d9ba5a574d6dec19d519692683027135',
salt: 'cf0bcf9bd942010fba7bfd1bcde96ae93b2e1a55f078a70b1cb48f000551c4e1',
email: 'email'
}
undefined
POST /sign - - ms - -
(Also would like to ask about that deprecation warning but I don't think it's related to the main topic of this post)
It seems like you user is saved to the database, save
won't return the inserted document
await newUser.save((err) => {
if (err) {
///....
}
also you have to check if the user exist in the database before hand
User.find({ email: email, (err, user) =>{
if (err) throw err
if(user) return console.log("user already exist")
//... your code
const newUser = new User({ username: username, password: hash, salt: salt, // construct a document
email: email });
newUser.save()
});
to make mongoose deprecated errors disappear add this lines
mongoose.set('useUnifiedTopology', true)
mongoose.set('useNewUrlParser', true)
mongoose.set('useFindAndModify', false)
Fixed it. Instead of using createConnection() I used mongoose.connect() and when I needed the object Connection I used mongoose.connection . Weird af I think and I would love to know the reason but if it works it works.
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.