javascriptnode.jsnode-sqlite3

Having some problems trying to call a function from another script


I'm building a mockup website to try and learn NodeJS. I want a login system and I'm trying to connect my register page with my database script. The sql function that sends queries to the database is working as intended, however, when trying to call the query function from the script that manages the register webpage all I get is an error 500.

It would be cool if someone could point me in the right direction, surely it's some quirk from NodeJS I don't know about yet.

Here is my register page script that should call the query function from POST routing:


var express = require('express');
var router = express.Router();
var db = require('../public/javascripts/dbController');

router
    .get('/', function(req, res, next) {
        res.render('register.html', {title: 'Register'})
    })
    .post('/', function(req, res, next) {
        register(req.body);
        res.render('register.html', {title: 'Register'})
    })

function register(request)
{

    let username = request.login;
    let password = request.password;

    let sql = "INSERT INTO users (user_username, user_password, user_status) VALUES ('"+username+"','"+password+"', 1);";
    console.log("query");
    //Why is this not working?
    db.query(sql);
}

module.exports = router;

And here is (part of) my dbController script:


const sqlite3 = require('sqlite3').verbose();

let db = new sqlite3.Database('./public/database/db.db', sqlite3.OPEN_READWRITE, (err) => {
    if (err && err.code == "SQLITE_CANTOPEN") {
        createDatabase();
        return;
        } else if (err) {
            console.log("Getting error " + err);
            exit(1);
    }
});

//This function is not running when I ask for it in register.js
function query(sql){

  console.log("running query: " + sql)

  db.all(sql, [], (err, rows) => {
    if (err) {
      throw err;
    }
    rows.forEach((row) => {
      console.log(row.name);
    });
  });

}

module.exports = query;

I figure that I probably have to route my scripts through the main app script or maybe I'm exporting wrong? Anyway, any nudge in the right direction would be great because I've been stuck on it a few days. Thanks!


Solution

  • For what I can see, you're indeed importing the "query" function into your "register" page. But you're setting a name of "db" to it.

    var db = require('../public/javascripts/dbController');
    

    but you're not exporting "db" you're exporting "query":

    module.exports = query;
    

    But that's not really the issue, you could just call it "myRandomNameImport" and it would still work. The problem is that you're accessing a property of "db" that does not exist.

    db.query(sql); /* <- db.query does not exist.
                    * Try db(sql) instead. */
    

    "db" does not have any properties called "query", the function you're trying to use is "db".

    function register(request) {
    
      let username = request.login;
      let password = request.password;
    
      let sql = "INSERT INTO users (user_username, user_password, user_status) VALUES ('"+username+"','"+password+"', 1);";
      console.log("query");
      db(sql); /*<- Just call db()*/
    }