Assigning result from a function callback inside for-loop, loop end and the object does not contain the...
This question already has an answer here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
6 answers
How do I return the response from an asynchronous call?
33 answers
Im having a trouble assigning a value to a object does not contain the value. I have a module that handle database query then I call it in side a for-loop.
socket.on('get_mail',function(data){
var obj = JSON.parse(data);
var mail = {inbox:};
for(var i = 0; i < obj.contacts.length; i++){
var params = {user1: obj.user, user2: obj.contacts[i].username};
var conversation = {username: obj.contacts[i].username, message:};
dbHandler.GetMessage(params, function(result){
conversation.message = result;
});
mail.inbox.push(conversation);
}
console.log(mail);
});
DBHandler.js
exports.GetMessage = function(data, callback){
var sql = `Call GetMessage(?, ?)`;
var result = ;
connection.query(sql, [data.user1, data.user2], function(error, results, fields){
if(error){
console.log(error.message);
}
if(results.length){
for(var i = 0; i < results[0].length; i++){
var message = {
mail_id: results[0][i].mail_id,
message_content: results[0][i].message_content,
sender: results[0][i].sender,
reciever: results[0][i].receiver,
mail_sent_date: results[0][i].mail_sent_date,
};
result.push(message);
}
return callback(result);
}
else{
console.log("No results found!")
}
});
};
javascript node.js socket.io
marked as duplicate by CertainPerformance
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 6:13
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
6 answers
How do I return the response from an asynchronous call?
33 answers
Im having a trouble assigning a value to a object does not contain the value. I have a module that handle database query then I call it in side a for-loop.
socket.on('get_mail',function(data){
var obj = JSON.parse(data);
var mail = {inbox:};
for(var i = 0; i < obj.contacts.length; i++){
var params = {user1: obj.user, user2: obj.contacts[i].username};
var conversation = {username: obj.contacts[i].username, message:};
dbHandler.GetMessage(params, function(result){
conversation.message = result;
});
mail.inbox.push(conversation);
}
console.log(mail);
});
DBHandler.js
exports.GetMessage = function(data, callback){
var sql = `Call GetMessage(?, ?)`;
var result = ;
connection.query(sql, [data.user1, data.user2], function(error, results, fields){
if(error){
console.log(error.message);
}
if(results.length){
for(var i = 0; i < results[0].length; i++){
var message = {
mail_id: results[0][i].mail_id,
message_content: results[0][i].message_content,
sender: results[0][i].sender,
reciever: results[0][i].receiver,
mail_sent_date: results[0][i].mail_sent_date,
};
result.push(message);
}
return callback(result);
}
else{
console.log("No results found!")
}
});
};
javascript node.js socket.io
marked as duplicate by CertainPerformance
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 6:13
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
UsePromise.all
on thecontacts
mapped to Promises instead
– CertainPerformance
Nov 22 '18 at 6:13
where to use promise.all in the code?
– Adrian Manuel Cleofe
Nov 22 '18 at 6:30
add a comment |
This question already has an answer here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
6 answers
How do I return the response from an asynchronous call?
33 answers
Im having a trouble assigning a value to a object does not contain the value. I have a module that handle database query then I call it in side a for-loop.
socket.on('get_mail',function(data){
var obj = JSON.parse(data);
var mail = {inbox:};
for(var i = 0; i < obj.contacts.length; i++){
var params = {user1: obj.user, user2: obj.contacts[i].username};
var conversation = {username: obj.contacts[i].username, message:};
dbHandler.GetMessage(params, function(result){
conversation.message = result;
});
mail.inbox.push(conversation);
}
console.log(mail);
});
DBHandler.js
exports.GetMessage = function(data, callback){
var sql = `Call GetMessage(?, ?)`;
var result = ;
connection.query(sql, [data.user1, data.user2], function(error, results, fields){
if(error){
console.log(error.message);
}
if(results.length){
for(var i = 0; i < results[0].length; i++){
var message = {
mail_id: results[0][i].mail_id,
message_content: results[0][i].message_content,
sender: results[0][i].sender,
reciever: results[0][i].receiver,
mail_sent_date: results[0][i].mail_sent_date,
};
result.push(message);
}
return callback(result);
}
else{
console.log("No results found!")
}
});
};
javascript node.js socket.io
This question already has an answer here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
6 answers
How do I return the response from an asynchronous call?
33 answers
Im having a trouble assigning a value to a object does not contain the value. I have a module that handle database query then I call it in side a for-loop.
socket.on('get_mail',function(data){
var obj = JSON.parse(data);
var mail = {inbox:};
for(var i = 0; i < obj.contacts.length; i++){
var params = {user1: obj.user, user2: obj.contacts[i].username};
var conversation = {username: obj.contacts[i].username, message:};
dbHandler.GetMessage(params, function(result){
conversation.message = result;
});
mail.inbox.push(conversation);
}
console.log(mail);
});
DBHandler.js
exports.GetMessage = function(data, callback){
var sql = `Call GetMessage(?, ?)`;
var result = ;
connection.query(sql, [data.user1, data.user2], function(error, results, fields){
if(error){
console.log(error.message);
}
if(results.length){
for(var i = 0; i < results[0].length; i++){
var message = {
mail_id: results[0][i].mail_id,
message_content: results[0][i].message_content,
sender: results[0][i].sender,
reciever: results[0][i].receiver,
mail_sent_date: results[0][i].mail_sent_date,
};
result.push(message);
}
return callback(result);
}
else{
console.log("No results found!")
}
});
};
This question already has an answer here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
6 answers
How do I return the response from an asynchronous call?
33 answers
javascript node.js socket.io
javascript node.js socket.io
edited Nov 22 '18 at 6:32
Adrian Manuel Cleofe
asked Nov 22 '18 at 6:12


Adrian Manuel CleofeAdrian Manuel Cleofe
188
188
marked as duplicate by CertainPerformance
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 6:13
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by CertainPerformance
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 6:13
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
UsePromise.all
on thecontacts
mapped to Promises instead
– CertainPerformance
Nov 22 '18 at 6:13
where to use promise.all in the code?
– Adrian Manuel Cleofe
Nov 22 '18 at 6:30
add a comment |
UsePromise.all
on thecontacts
mapped to Promises instead
– CertainPerformance
Nov 22 '18 at 6:13
where to use promise.all in the code?
– Adrian Manuel Cleofe
Nov 22 '18 at 6:30
Use
Promise.all
on the contacts
mapped to Promises instead– CertainPerformance
Nov 22 '18 at 6:13
Use
Promise.all
on the contacts
mapped to Promises instead– CertainPerformance
Nov 22 '18 at 6:13
where to use promise.all in the code?
– Adrian Manuel Cleofe
Nov 22 '18 at 6:30
where to use promise.all in the code?
– Adrian Manuel Cleofe
Nov 22 '18 at 6:30
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use
Promise.all
on thecontacts
mapped to Promises instead– CertainPerformance
Nov 22 '18 at 6:13
where to use promise.all in the code?
– Adrian Manuel Cleofe
Nov 22 '18 at 6:30