最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - callback is not a function in node js - Stack Overflow

programmeradmin5浏览0评论

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.

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.

Share Improve this question edited Jan 4, 2017 at 5:19 Aruna 12k3 gold badges30 silver badges42 bronze badges asked Jan 4, 2017 at 5:00 VisheshRajuVisheshRaju 3482 gold badges4 silver badges18 bronze badges 5
  • 1 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 – Jaromanda X Commented Jan 4, 2017 at 5:02
  • I get it. but how do i add the function callback in my logic ? – VisheshRaju Commented Jan 4, 2017 at 5:05
  • I've added an answer that should help – Jaromanda X Commented Jan 4, 2017 at 5:05
  • Also, the callback is being returned the error and result both in the first argument. Change this callback(row); to callback(null, row); and handle the same in the calling method. – Aruna Commented Jan 4, 2017 at 5:07
  • I have used a logic simillar to this ... var sum1 = function (a,b, callback){ callback(a + b); }; – VisheshRaju Commented Jan 4, 2017 at 5:09
Add a ment  | 

2 Answers 2

Reset to default 5

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

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);
    });
  });
});
发布评论

评论列表(0)

  1. 暂无评论