Assigning result from a function callback inside for-loop, loop end and the object does not contain the...












0
















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


};










share|improve this question















marked as duplicate by CertainPerformance javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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.



















  • 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
















0
















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


};










share|improve this question















marked as duplicate by CertainPerformance javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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.



















  • 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














0












0








0









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


};










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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.















  • 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



















  • 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

















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












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory