1

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?

2 Answers 2

2

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');
});
Sign up to request clarification or add additional context in comments.

4 Comments

I don't think this will work. I want to embed the db.get() into another function and so I will need the "outer" function to return if db.get() returns a non-empty entry. What you've proposed will only return the callback function inside db.get() and not my "outer" function.
Actually I realised I was being silly. Your solution works. I just defined the variable outside db.get and update it using the callback function.
@Zeta-Squared I'm happy it worked for you, please accept my answer it helped you
Actually it did not work. I keep getting a result which is undefined even though I know the database has a matching entry. The main post will be edited.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.