I have a POST request with the user credentials as an object from the login page and transferred to the API server like this:
loginUser(creds) { //creds is in the form of { username: bob, password: 123 } var request = { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(creds), } fetch(`http:`, request) .then(res => res.json()) .then(user => { console.log(user); console.log('Successful') }) .catch(err => { console.log('Error is', err) }) },
And the API server gets it like this:
//With .findOne() app.post('/api/login/', function(req, res) { console.log('Req body in login ', req.body) db.collection('users').findOne(req.body, function(err, isMatch) { console.log('ISMATCH IS: ' + isMatch) if(err) { console.log('THIS IS ERROR RESPONSE') res.json(err) } else { console.log('THIS IS ISMATCH RESPONSE') res.json(isMatch) } }) })
or
//With .find() app.post('/api/login/', function(req, res) { console.log('Req body in login ', req.body) //console logs correctly as { username: bob, password: 123 } db.collection('users').find(req.body).next(function(err, isMatch) { console.log('ISMATCH IS: ' + isMatch) if(err) { console.log('THIS IS ERROR RESPONSE') res.json(err) } else { console.log('THIS IS ISMATCH RESPONSE') res.json(isMatch) } }) })
Thus, with the login credentials provided, inside the API server, I would like to search my 'users' database to see if any of them match. But in both cases, isMatch always null and always logs console.log('THIS IS ISMATCH RESPONSE') , even if the user credentials do not match any of the databases. And on the client side, I never get any error responses and console.log('Successful') always logged.
It may not seem that I am missing. What can i do wrong?
thanks
user3259472
source share