I want to check, if a user is already in my database (sqlite3).
- If the user exists: return 500 (ERROR!!)
- If the user not exists: return 200 (ok)
Thats my script: node.js+express on serverside.
app.post('/adduser', function(req,res){
db.serialize(function(){
var user = req.body.user;
var password = req.body.password;
var err = false;
db.each("SELECT name FROM users", function(err, row) {
if (user == row.name) {
console.log(row.name);
err = true;
console.log("a: " +err);
res.send(500);
return false;
}
});
if (err==false) {
console.log("b: " +err);
res.send(200);
}
})
});
I get always the success code 200 regardless of whether I submitting an existing username or not. Thats the log from server:
b: false
testuser
a: true
I've entered an exisiting user, but it got code 200. Strange, I get b before a.
db.eachdoesn't (since it takes a callback) seem to run synchronously, so yourif(err==false)line will execute long before getting the first result from the database.erris the database error, rather than record not found. Amend your firstifinstead.