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.
-
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 thiscallback(row);
tocallback(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
2 Answers
Reset to default 5As 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);
});
});
});