3

When I run the application, It shows me Database is connected!

db.js

var mysql = require('mysql');
var settings = require('./config');
var db;
var exports = {};

exports.connectdb = function () {

 db = mysql.createConnection(settings.Database);

 db.connect(function(err){

     console.log('connecting');
     if(!err) {
         console.log('Database is connected!');
         return db;
     } else {
         console.log('Error connecting database!'+err);
         return null;
     }
 });

};

module.exports = exports;

but when i am trying to connect DB from user.js it shows me connection is null / TypeError: Cannot read property 'query' of undefined.

code block from user.js

var exports = {};
var dbcon = require('../config/db.js');
var dbconn = dbcon.connectdb();

exports.login = function(email,password) {

var userdetails = { name:email, password:password};

var dbconn = dbcon.connectdb();
if ( dbconn == null ) console.log('still nul');


dbconn.query("SELECT * FROM users where email = '"+email+"' and password = '"+password +"'", function (err, result) {
    if(err)
    {
        console.log(result[0]+' err');
        return null;
    }
});
};

module.exports = exports;
4
  • Can you post more code in your user.js file? Preferably, to include your require statements. Commented Nov 25, 2016 at 15:43
  • yes! check it now Commented Nov 25, 2016 at 15:47
  • i think it connect for the first time.. when i load application... but when i try to login through select query that time connection variable is empty.. so i make an object of db then call connect function ..... my connectdb function it called but db.connect() inside function is not showing any result.. example ( "error connecting DB" OR "Database is connected!" ) Commented Nov 25, 2016 at 15:50
  • I posted an answer. Let me know if that works for you or if you have additional questions. Commented Nov 25, 2016 at 16:05

1 Answer 1

2

Node.js is asynchronous by nature. You are trying to use it in a synchronous fashion. To make this work, you must use the callback pattern. Below is an example:

db.js

var mysql = require('mysql');
var settings = require('./config');
var exports = {};

exports.connectdb = function (callback) {

 var db = mysql.createConnection(settings.Database);

 db.connect(function(err){
     callback(err,db);
 });

};

module.exports = exports;

user.js

var exports = {};
var dbcon = require('../config/db.js');

exports.login = function(email,password) {

    var userdetails = { name:email, password:password};

    dbcon.connectdb(function(err, dbconn){
        if ( err) //handle error


        dbconn.query("SELECT * FROM users where email = '"+email+"' and password = '"+password +"'", function (err, result) {
        if(err)
        {
           console.log(result[0]+' err');
        }
        });
    });
};

module.exports = exports;

From the code above you can see how the connectdb function accepts a function callback. When the database is connected the code will execute that callback to send the results. In the user.js file, you can now pass a callback function and use the results it gives you (the db). You can find more info about Node.js' asynchronous nature here. Basically, asynchronous code uses callbacks and synchronous code uses return statements. Returning values from asynchronous functions will most always yield null results as asynchronous callbacks will always fire "sometime" after the function is called.

Sign up to request clarification or add additional context in comments.

6 Comments

TypeError: callback is not a function
That means that callback you passed is not a function. Do you know where that error is coming from? The code I posted doesn't appear to have any errors.
i am new to nodeJS but when i am trying to call callback function after connect() it is obvious i don't have any call back function that why it showing me error, also let me know if you have any example of login system
When you call dbcon.connectdb(function(err, dbconn){ ..., the function is the callback referenced in db.js on the line exports.connectdb = function (callback) {. So, callback is a function and it is being used correctly in the example. Can you update your code example with what you have?
In the user.js you posted above. you still did not change the code. In your example you have var dbconn = dbcon.connectdb(); and in mine I have dbcon.connectdb(function(err, dbconn){ .... You need to update your code using my example.
|

Your Answer

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