Tuesday 15 May 2018

Need help understanding and converting synchronous dependant MySQL query code

Hello, this is my second Node project. I am using Node, Express, MySQL.What I am doing is, I have an array of names of users that have posted something, I then loop over those names and for each of them I do a connection.query to get their posts and I store those into an array(after that I do some other data manipulation to it, but that's not the important part)The problem is: my code tries to do that data manipulation before it even receives the data from the connection.query!I google-d around and it seems async await is the thing I need, problem is, the examples given are quite different from my situation and I can't really find code that fits my situation. // namesOfPeopleImFollowing is the array with the names namesOfPeopleImFollowing.forEach(function(ele){ connection.query(`SELECT * FROM user${ele}posts`, function(error,resultOfThis){ if(error){ console.log("Error found" + error) } else { allPostsWithUsername.push([{username:ele},resultOfThis]) } }) }) console.log(JSON.stringify(allPostsWithUsername)) // This is EMPTY, it mustn't be empty. So, how do I convert that into something which will work properly?(Incase you need the entire code, here it is: https://ift.tt/2jXQvSf though I forgot to uncomment the code)Thank you for your time.

Submitted May 15, 2018 at 10:59AM by khazha88

No comments:

Post a Comment