JavaScript/NodeJS Callback within a callback [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
I'm using the 'user-management' package in Node, and I have a callback within a callback, within a callback. But the final result doesn't return. This is my main NodeJS module:
playerManagement.login(data.username, data.pw, function (result) {
console.log(result) <-- statement never reached
if (result == "fail") {
socket.emit('client', { type: 'login', result : 'fail'});
} else {
connections[playerindex++] = {'username' : username, 'sockid' : socket.id, 'token' : result };
socket.emit('client', { type: 'login', result : 'success', username : username });
console.log(connections);
}
});
Then I have an external module with the function:
playerModule.prototype.login = function(username, password) {
var o = this;
o.user.load(function (err) {
if (!err) {
o.user.authenticateUser(username, password, function(err, result) {
if (!result.userExists) {
console.log('Invalid username');
return "fail";
} else if (!result.passwordsMatch) {
console.log('Invalid password');
return "fail";
} else {
console.log('User token is: ' + result.token); <--- this is reached.
return result.token;
}
});
} else {
console.log('error logging in');
return "fail";
}
});
So I'm guessing I need to return the value to the "load" function callback, but I'm not sure how to do that.

Change the definition of login with the following.
playerModule.prototype.login = function(username, password, callback) {
var o = this;
o.user.load(function (err) {
if (!err) {
o.user.authenticateUser(username, password, function(err, result) {
if (!result.userExists) {
console.log('Invalid username');
return callback("fail");
} else if (!result.passwordsMatch) {
console.log('Invalid password');
return callback("fail");
} else {
console.log('User token is: ' + result.token); <--- this is reached.
return callback(result.token);
}
});
} else {
console.log('error logging in');
return callback("fail");
}
});

Related

how can i get value out of a child function [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
var checkAcount = (usr, pass) => {
var sql = "SELECT * FROM account WHERE userName = '" +usr+"'" ;
con.query(sql, (err, result) => {
if(err) throw err;
if(result.length > 0){
bcrypt.compare(pass, result[0].password, function(err, result1) {
if(result1 == true){
return true;
}
else{
return false;
}
});
}
else {
return false;
}
});
return ???;
}
I have code like this and I don't know how to make this function return the value of the compare function (true or false). Add return in child function like I did seem not to work. Can somebody help me please.
You could return a promise:
async function checkAcount(usr, pass) => {
const sql = "SELECT * FROM account WHERE userName = ?" ;
return new Promise((resolve, reject) => {
con.query(sql, [usr], (err, result) => {
if(err) {
reject(err);
throw err;
}
if(result.length > 0){
bcrypt.compare(pass, result[0].password, function(err, result1) {
if(result1 == true){
resolve(result);
return true;
} else{
reject(err);
return false;
}
});
} else {
reject(err);
return false;
}
});
});
}
Don't build your SQL query with string concatenation. That allows SQL injection.
bcrypt returns a promise
https://www.npmjs.com/package/bcrypt#with-promises
bcrypt.compare(pass, result[0].password).then((result) => {
return result;
})

Return value from callback in TypeScript [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 years ago.
I have a function in typescrypt in file Service.ts:
export const doCallAuth = (username, password) => {
var auth = new Auth({
url: '...',
});
var status;
auth.authenticate(username, password, function (err, user) {
if (err) {
console.log(err);
status = 'no';
} else if (!user.uid) {
console.log("user not found Error");
status = 'no';
} else if (user.uid) {
console.log("success : user " + user.uid + " found ");
status = 'yes';
}
});
return status;
}
I call this method by :
var result = Service.doCallAuth('test', 'test');
And variable result is undefined
I don't know why result is undefined
Can anybody help me out ?
Thanks in advance. :)
You can use Promise or callback patterns
1. Promise
export const doCallAuth = (username, password) => {
return new Promise((resolve, reject) => {
var auth = new Auth({ url: "..." });
auth.authenticate(username, password, (err, user) => {
if (err) {
reject(err);
} else if (!user.uid) {
reject(new Error("user not found Error"));
} else if (user.uid) {
console.log("success : user " + user.uid + " found ");
resolve(user);
}
});
});
};
Service.doCallAuth("test", "test")
.then(user => {
console.log("success : user " + user.uid + " found ");
})
.catch(e => {
console.log(e);
});
2. Callback
export const doCallAuthCallBack = (username, password, callback) => {
var auth = new Auth({ url: "..." });
auth.authenticate(username, password, (err, user) => {
if (err) {
callback(err);
} else if (!user.uid) {
callback(new Error("user not found Error"));
} else if (user.uid) {
callback(null, user);
}
});
};
Service.doCallAuthCallBack("test", "test", (err, user) => {
if (err) {
console.log(err);
} else {
console.log("success : user " + user.uid + " found ");
}
});
Try this one.
export const doCallAuth = (username, password, callback) => {
var auth = new Auth({
url: '...',
});
var status;
auth.authenticate(username, password, callback);
}
var result;
Service.doCallAuth('test', 'test', function (err, user) {
if (err) {
console.log(err);
status = 'no';
} else if (!user.uid) {
console.log("user not found Error");
status = 'no';
} else if (user.uid) {
console.log("success : user " + user.uid + " found ");
status = 'yes';
}
result = status;
});
To get value from AJAX call need to Use async and await
export const doCallAuth = async function(username, password) => {
var auth = new Auth({
url: '...',
});
var status;
auth.authenticate(username, password, function (err, user) {
if (err) {
console.log(err);
status = 'no';
} else if (!user.uid) {
console.log("user not found Error");
status = 'no';
} else if (user.uid) {
console.log("success : user " + user.uid + " found ");
status = 'yes';
}
return status;
});
}
call this method by :
var result = await Service.doCallAuth('test', 'test');

Callback Function returns undefined somehow [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
api.js:
var db = require('./db.js');
console.log(searchAccessToken(name,queryDB));
searchAccessToken function:
function searchAccessToken(emailAddress,callback){
var sql = "SELECT accessToken FROM device WHERE email_address = '" + emailAddress + "'";
return callback(sql);
}
queryDB function:
function queryDB(sql){
var token = "";
db(function(err,conn){
conn.query(sql,function(err,results){
if(err){
conn.release();
return err;
}else if(results.length){
token = results[0].accessToken;
}
conn.release();
return token;
});
});
}
db.js
var mysql = require('mysql');
var pool = mysql.createPool({
//development
// host:'localhost',
// user:'root',
// password: '',
// database: 'merchantdev',
// insecureAuth : true
//staging
host:'',
user: '',
password: '',
database: ''
});
var getConnection = function(callback) {
pool.getConnection(function(err, connection) {
if(err) {
return callback(err);
}
callback(null, connection);
});
};
module.exports = getConnection;
I have read the other thread in SOF (How do I return the response from an asynchronous call?) but still unable to find out the mistake in my code. Please do not close this question as I have struggled for hours to find the solution but still couldn't. Console.log returns undefined when it is supposed to return a value.
Your callback function flow is not correct, you are calling asynchronous function that is conn.query
var db = require('./db.js');
// this function not required callback function
// as we are not doing anything asynchronous
function searchAccessToken(emailAddress){
var sql = "SELECT accessToken FROM device WHERE email_address = '" + emailAddress + "'";
return sql;
}
// this function required callback function
// as we are calling 2 asynchronous function
function queryDB(sql, callback){
var token = "";
db(function(err,conn){
conn.query(sql,function(err,results){
if(err){
conn.release();
return callback(err);
}else if(results.length){
token = results[0].accessToken;
}
conn.release();
return callback(null, token);
});
});
}
queryDB(searchAccessToken(name), function callback(err, token) {
if(err) return console.log(err)
console.log(token);
})
Checkout this snippet with callback error handling.
var db = require('./db.js');
function queryDB(sql, callback){
var token = "";
db(function(err,conn){
conn.query(sql,function(err,results){
if(err){
conn.release();
callback(err);
}else if(results && results.length > 0){
token = results[0].accessToken;
}
conn.release();
callback(null, token); //Returning Token as callback response
});
});
}
function searchAccessToken(emailAddress,callback){
var sql = "SELECT accessToken FROM device WHERE email_address = '" + emailAddress + "'";
queryDB(sql, (err, token) => {
if(err) {
callback(err);
} else {
callback(null, token);
}
})
}
searchAccessToken("xyz#gmail.com", (err, token) => {
if(err) {
console.log("Err :", err);
} else {
console.log("Token :", token);
}
});

Callback function not working in my post request

I have this code with callbacks:
function getUserToken(data, callback) {
var password_sha256 = sha256(data.password);
getAppById(data.app_id).then(function(res) {
console.log("app"+res);
if (!res) {
console.log("No app");
callback(err, { meta: {
code: 403,
error_message: "There are no app with your id!"
} });
} else {
if (res.user_password == password_sha256) {
console.log("user found");
callback(err, { meta: { code: 200 },
token: password_sha256,
type: 1 });
return;
} else if (res.owner_password == password_sha256) {
console.log("owner found");
callback(err, { meta: { code: 200 },
token: password_sha256,
type: 0 });
} else {
console.log("user not found");
callback(err, { meta: {
code: 403,
error_message: "There are no users with your password!"
} });
}
}
});
}
I post some data using this function:
router.post('/api/login', (req, res) => {
db.getUserToken(req.body, function(err, result) {
console.log(result);
if (result.error) {
return res.status(403).send(result);
} else {
return res.status(200).send(result);
}
});
});
And then getUserToken found, for example, user, so I have "user found" in console log, but callback function in /api/login not working. Where is my error and how can I fix them?
Instead of callback, you can return a Promise, either native or using an external library like Kris Kowal Q
var Q = require('q');
function getUserToken(data) {
var deferred = Q.defer();
var password_sha256 = sha256(data.password);
getAppById(data.app_id).then(function(res) {
if (!res) {
deferred.reject("There are no app with your id!");
} else {
if (res.user_password == password_sha256) {
deferred.resolve({
token: password_sha256,
type: 1
});
} else if (res.owner_password == password_sha256) {
deferred.resolve({
token: password_sha256,
type: 0
});
} else {
console.log("user not found");
deferred.reject("There are no users with your password!");
}
}
})
.catch(function(error) {
deferred.reject(error);
});
return deferred.promise;
}
Then simply process or catch error when you post data
router.post('/api/login', (req, res) => {
db.getUserToken(req.body)
.then(function(result) {
res.status(200).send(result);
})
.catch(function(error) {
res.status(403).send(error);
});
});
Instead of using callback(err, data) you should use callback(null,data).This will give you required output.

How to wait for results inside an if statement in js?

I'm bulding a node.js backend for a webapp and when i submit a form, i do various validations. One of them is to check if an invitation already exists with the same email address.(isUserAlreadyInvited function)
I created a function for this, however when i call this, i guess the response is not that fast and it just moves to the next statement even if the check returns true. How
//Loop through emails one by one
var emails_to_invite = ["test#test.com","invalid.com"];
var response_items = [];
async.each(emails_to_invite,
function(email, callback){
//Response item
var response_item = {}
response_item.email = email;
//Validate
if(Joi.validate({ email: email }, apiSchema).error) {
response_item.error = "not valid email";
response_items.push(response_item);
callback();
} else if(email == user.email) {
response_item.error = "Sending an invitation to your own email is kinda dumb";
response_items.push(response_item);
callback();
} else if(isUserAlreadyInvited(email,user_id)) {
response_item.error = "already invited";
response_items.push(response_item);
callback();
} else {
sendInvitationEmail(email,user_id,null,null,function(invitation_code){
if(invitation_code) {
response_item.invitationCode = invitation_code;
} else {
response_item.error = "server error";
}
response_items.push(response_item);
callback();
});
};
},
function(err){
//Return response
res.status(200).send(response_items);
}
);
function isUserAlreadyInvited(email,invited_by) {
User.findOne({
email: email,
invited_by: invited_by
}, function(err, user) {
if(err) {
return false;
} else {
return true;
}
});
}
Two returns here:
function isUserAlreadyInvited(email,invited_by) {
User.findOne({
email: email,
invited_by: invited_by
}, function(err, user) {
if(err) {
return false;
} else {
return true;
}
});
}
are defined inside this function
function(err, user) {
if(err) {
return false;
} else {
return true;
}
}
and have nothing with return value of isUserAlreadyInvited().
Apparently your isUserAlreadyInvited() is an asynchronous function that needs to be treated asynchronously - it shall get callback function as a parameter. That callback shall be invoked in place of that function(err, user) {}

Categories

Resources