Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. mel.New({
  2. route: '/forum/category/get/all',
  3. run: (args, c) => {
  4. var category = [];
  5. var nodes = [];
  6. var ret = [];
  7.  
  8.  
  9. katjs.Query("SELECT * FROM forum_categories")
  10. .then( category_ => {
  11. category = category_; // no need to do this, just use category_ but that is the least of the issues lol
  12. for (var value in category){ // async so having a query inside of this will not work
  13. var dev3 = [];
  14. katjs.Query(`SELECT * FROM forum_nodes WHERE category = :category`, {category: value['id']} ) // this query finishes after you output so pointless
  15. .then( dev3_ => {
  16. dev3 = dev3_; // same again ^ you can just use dev3_
  17. for (var dnode in dev3) {
  18. var latest, discussions, discussions2 = [];
  19. var talk = 0;
  20.  
  21. katjs.Query(`SELECT * FROM forum_discussions WHERE nodeid = :nodeid ORDER BY id DESC Limit 1`, {nodeid:dnode['id']}) //
  22. .then ( latest_ => {
  23. latest = latest_; // what is the point in setting latest when all the other code has already ran after this is completed
  24. });
  25. katjs.QueryValue(`SELECT COUNT(*) FROM forum_discussions WHERE nodeid = :category`, {category:dnode['id']})
  26. .then (dis_ =>{
  27. discussions = dis_; // same again
  28. });
  29. katjs.Query(`SELECT * FROM forum_discussions WHERE nodeid = :category`, {category:dnode['id']})
  30. .then( dis2_ => {
  31. discussions2 = dis2_; // and again
  32. });
  33.  
  34.  
  35.  
  36.  
  37. for (var dis in discussion2){ // discussions2 is empty, 'discussions2 = dis2_;' runs after this is ran
  38. katjs.QueryValue("SELECT COUNT(*) FROM forum_messages WHERE discussionid = :category", {category:dis['id']} ) // will never get here
  39. .then (talk_ => {
  40. talk += talk_;
  41. console.log("talk: "+talk);
  42. })
  43. }
  44.  
  45.  
  46. for (var latest2 in latest){
  47. katjs.QueryValue("SELECT username FROM users WHERE id = :id", {id:latest2['userid']} ) //
  48. .then(user_ => {
  49. latest2['username'] = user_;
  50. });
  51. }
  52.  
  53. // the last 5+ queries will run at the same time and .then will be called AFTER everything below has already been ran so you can remove them and the result would be the same
  54.  
  55. dnode['latest'] = latest; // latest is empty when this is ran
  56. dnode['discussions'] = discussions; // discussions is empty when this is ran
  57. dnode['messages'] = talk; // talk is also empty when this is ran
  58.  
  59. }
  60.  
  61. value['nodes'] = dev3;
  62. category[value['id']-1] = value;
  63.  
  64. })
  65.  
  66. }
  67.  
  68. c(mel.Success({category:category})) // again, runs after the second query is ran so what is the point
  69. // also, ever heard of JOIN?? this is way more queries it could probably be done in 1.
  70. });
  71. }
  72. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement