Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

callback is not a function in node js

I am trying a simple operation that will result the particular users details from the database. Pooling database and all other connections work perfectly but the callback is not working. Am I doing anything wrong here?

Below is the code I use.

db_crud.js

var express = require('express');
var app = express();

var crud = require('./routes/crud_op_new.js');
var search = require('./routes/search.js');

var connection;
var result;
app.get('/search',(req,res)=>{
  crud.connection(function (con) {
    search.getuser(con,req.param('name'),result);
    res.send(result);
  });
});
app.listen(8003);

Finally here is where the error occurs ... search.js

exports.getuser = function(connection,req,callback){
console.log("GET Request iniciated");
connection.query("select * from user,addr where name=? and user.id=addr.e_id",[req],(err,row)=>{
 if(err){
    callback("DB ERROR: "+err);
 }
 else {
   if(row.length==0){
   callback("No Records found");
  }
   else {
    callback(row);
   }
  }
 });
}

The db_crud will send the credentials to search.js and here the callback is called to send result. crud_op_new.js creates the db pool connection and is in variable con.

like image 504
VisheshRaju Avatar asked Jan 04 '17 05:01

VisheshRaju


2 Answers

As mentioned by Jaromanda X in the answer, result is just declared and unassigned which should be a callback function.

Also, the callback in search.js is being returned the error and result both as the first argument. You have to change this callback(row) to callback(null, row) to handle the error and result as below.

Note: Best practice in node js callback function would be, first argument should return an error (null in case of no error) and then remaining arguments can be the return values.

db_crud.js

var express = require('express');
var app = express();

var crud = require('./routes/crud_op_new.js');
var search = require('./routes/search.js');

var connection;

app.get('/search',(req,res)=>{
  crud.connection(function (con) {
    search.getuser(con,req.param('name'), function(err, result) {
      if(err) {
         res.status(501).send(err);
      } else {
        res.send(result);
      }
    });
  });
});
app.listen(8003);

search.js

exports.getuser = function(connection,req,callback){
console.log("GET Request iniciated");
connection.query("select * from user,addr where name=? and user.id=addr.e_id",[req],(err,row)=>{
 if(err){
    callback("DB ERROR: "+err);
 }
 else {
   if(row.length==0){
   callback("No Records found");
  }
   else {
    callback(null, row);
   }
  }
 });
}
like image 173
Aruna Avatar answered Sep 20 '22 17:09

Aruna


you call your function search.getuser(con,req.param('name'),result); ... result is not a function, it's undefined ... a callback needs to be a function so it can be called back

This should work

app.get('/search',(req,res)=>{
  crud.connection(function (con) {
    //                                   vvvvvvvvvvvvvvv this is the callback function
    search.getuser(con,req.param('name'),function(result) {
      res.send(result);
    });
  });
});
like image 43
Jaromanda X Avatar answered Sep 21 '22 17:09

Jaromanda X