Reputation: 290
In general how does javascript interpret a Database {}
object? I am writing some back end scripts to handle a registration form verification. In particular I need to ensure that the username and email used to register is not currently in use. To do this I use the sqlite3 package and use a few db.get
calls to determine if there are existing entries in my database for the username and email used on the registration form. I want to use the return of db.get
to check if it is empty or not and use this conditional to perform the necessary task. However the db.get
returns a Database {}
object which I am unfamiliar of how to work with.
Hopefully the following pseudo describes the issue better. Here uname
returns Database {}
and so never fails the if statement.
function existance(username, email) {
let uname = db.get(sql, username, callback(err, throw));
if (uname) {
let errors = {username: 'Username already in use.'};
return errors;
}
};
EDIT
I have since used Gajiu's recommendation but still having issues. So I have two files:
registrant_existence.js
// necessary requirements and initialisation
function registrant_existence(username) {
let uname;
let sql = 'SELECT username FROM table WHERE username=?';
db.get(sql, username, function(err, row) {
if (err) {
throw err;
} else {
uname = row;
console.log(uname);
}
});
console.log(uname);
if (uname !== undefined) {
return {username: 'Username already in use.'};
} else {
return 'DNE';
}
};
module.exports = registrant_existence;
register.js
let registrant_existence = require("path to registrant_existence.js");
// necessary requirements and initialisation
router.post('/', function(req, res) {
let existence = registrant_existence(req.body.username, req.body.email);
if (existence != 'DNE') {
// render registration page notifying the user
// that the username is already in use
} else {
// register the new user details
}
});
The uname
variable is undefined always. I placed the console.log(uname)
in two spots in registrant_existence.js as is seen above to see what is happening. Two strange things occur.
The first is that the console.log(uname)
outside the db.get()
displays undefined
in the console and the console.log(uname)
inside the db.get()
displays the expected string (a username I know is in my database).
The second is that the console.log(uname)
outside the db.get()
is displayed before the console.log(uname)
_inside the db.get()
in my console.
I have no idea why these things are happening. Does anyone have any suggestions?
Upvotes: 1
Views: 893
Reputation: 2032
I guess you are looking for a wrapper around your Database object, something like an Object Relational Mapper (ORM), this one is used regularly https://sequelize.org/master/manual/getting-started.html
On the other hand for your specific use case you might want to get a look at this https://www.sqlite.org/lang_createtable.html#unique_constraints: unicity is usually via constraints in the data store.
Upvotes: 0
Reputation: 245
You should try something like that:
db.get(sql, username, (err, data) => {
// process the data here
if (err) {
return console.error(err.message);
}
return data
? console.log(data.id, data.userName)
: console.log('No username found');
});
Upvotes: 2