I'm learning React with Node (Express/Mongoose) and MongoDB and everything seems to work as expected now but I have this huge issue:
var userSchema = new mongoose.Schema({
login: { type: String, required: true, unique: true },
password: { type: String, required: true }
})
app.post('/registerUser', function (req, res) {
res.send(req.body)
db.on('error', console.error.bind(console, 'connection error:'))
// save user using model
var user = new UserModel(req.body)
user.save()
}
Every time I POST login and password it gets saved only once and then it's over.
So for example if I send POST three times:
login=admin&password=admin
login=admin2&password=admin2
login=admin3&password=admin3
db.users.count()
shows 1
and only the very first POST (admin:admin) is in the database. The rest is being ignored. I'm running console.logs to see what's wrong but every POST gets different _id, but somehow then do not save after the first one. I was thinking maybe there is something wrong with save
and maybe I should use something like update
or create
but every tutorial I've seen and the documentation says to use save
.
When I remove the field and the collection is empty and then send POST it saves immediately and then ignores every other POST (second, third, etc.).
What am I doing wrong? I want to have more than 1 user in my collection :(
via Wordpressor
No comments:
Post a Comment