Home > Net >  Handling database objects in node js
Handling database objects in node js

Time:10-18

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;
}

};

CodePudding user response:

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');
});

CodePudding user response:

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.

  • Related