0

I have a problem when working with node.js. All have tried to describe in comments to the code.

The first thing I need to build an array of dialogs with some information about the interlocutors and the last meggase.

IM = {
    iUserId: false,
    socket: false,
    checkDialog: function(socket) {
        this.socket = socket;
        // Returned [{owner: 123, viewer: 432}]
        var sql = 'SELECT DISTINCT(`owner_user_id`), `viewer_user_id` FROM `phpfox_mail` WHERE `owner_user_id` = ' + this.iUserId + ' GROUP BY 1,2 UNION SELECT DISTINCT (`viewer_user_id`), `owner_user_id` FROM `phpfox_mail` WHERE `viewer_user_id` = ' + this.iUserId + ' GROUP BY 1,2 ORDER BY 1 ';

        connection.query(sql, function(err, rows) {
          if (err) throw err;

          async.map(rows, function(item, nextParent) {
              var sql = 'SELECT `mail_id`, `subject`, `preview`, `owner_user_id`, `viewer_user_id`, `viewer_is_new`, `time_stamp` FROM `phpfox_mail` WHERE (`viewer_user_id` = ' + item.viewer_user_id + ' OR `owner_user_id` = ' + item.viewer_user_id + ') AND (`viewer_user_id` = ' + item.owner_user_id + ' OR `owner_user_id` = ' + item.owner_user_id + ') ORDER BY `mail_id` DESC LIMIT 1';
              var dialogs = [];
              connection.query(sql, function(err, rows) {
              // ???
              });

          }, function(err, item) {
              // Here I have to get the generated array with all the necessary dialogue.
              console.log(item);

              IM.socket.emit('logger', {text: 'dataIsABuilding', key: 'success'});
              IM.socket.emit('dialogsBuilding', item);
          });
        });
    }
};

Objective: To create an array of information about the message and interlocutors.

Scheme:

  1. Get an array with all interlocutors. [{owner_user_id: 5757, viewer_user_id: 5866}, {etc...}]
  2. Using the result of paragraph number 1, to get an array of information on the latest report from the dialogue. [{mail_id: 98, subject: test, owner: 5757, viewer: 5866, timestamp: 123566544}, {etc...}]
  3. Get information about users for the specified identifier (owner_user_id/viewer_user_id).
  4. Collect all the data on the dialogues in the same array.

I stopped at the third paragraph. Namely, I do not know how to consistently obtain information about users who are in the first two arrays.

Please, help my!

6
  • 3
    Love posts with no direct questions and tons of commented code to sort through... Ignoring the sarcasm, it would help if you would clean this up, a lot, and end one of your sentences with a question mark, preferably a new sentence containing some sort of inquiry. Asking questions in code comments is sketchy at best, and keeps the code highlighting from helping us read it! Not to mention I'm not sure if the commented code is part of the question your asking and the intent is for it to be fixed, or if you just forgot to delete it after copying and pasting from your machine. Commented Jul 16, 2013 at 13:34
  • @ChrisCM Got it. Now fix it. Commented Jul 16, 2013 at 13:40
  • There's still a large section of commented code... please at the very least, trim the code down (commented and uncommented included) to the minimum set of things you are attempting to run on your machine that is exhibiting the problem you have described. Commented Jul 16, 2013 at 13:48
  • @ChrisCM I think it is normal now? Commented Jul 16, 2013 at 14:07
  • Ah yes, it seems that your edit just didn't come through the first time, because this looks much better! Commented Jul 16, 2013 at 14:08

3 Answers 3

1

I don't believe you are using the async.map function correctly. Unfortunately you are using it so incorrectly that I cannot determine what it is you are actually trying to do. But here is an explanation of how you are misusing, vs the proper use of the async.map function.

Example from the async documentation:

async.map(['file1','file2','file3'], fs.stat, function(err, results){
    // results is now an array of stats for each file
});

Let's Simplify this a bit.

async.map(someArray, someFunction, someCallBack);

I believe your problem lies in the someFunction argument. Here is your version of this:

function(item, nextParent) {
    var sql = 'SELECT `mail_id`, `subject`, `preview`, `owner_user_id`, `viewer_user_id`, `viewer_is_new`, `time_stamp` FROM `phpfox_mail` WHERE (`viewer_user_id` = ' + item.viewer_user_id + ' OR `owner_user_id` = ' + item.viewer_user_id + ') AND (`viewer_user_id` = ' + item.owner_user_id + ' OR `owner_user_id` = ' + item.owner_user_id + ') ORDER BY `mail_id` DESC LIMIT 1';
    var dialogs = [];
    connection.query(sql, function(err, rows) {
        // ???
    });
}

The function for this argument, if we consult the fs.Stat example, should take two arguments, this first is an item from the array we sent into async.map. This part I believe you have right. HOwever, the second argument is a callback, with two arguments. An error, and a set of results. This is the part I think you are messing up. You should call this callback. Internally async.map provides the callback argument, this callback argument is how async.map collects results into the array. Without the call to this, async.map cannot collect the results.

I believe what you want is this:

function(item, someCallBackArgument) {
    var sql = 'SELECT `mail_id`, `subject`, `preview`, `owner_user_id`, `viewer_user_id`, `viewer_is_new`, `time_stamp` FROM `phpfox_mail` WHERE (`viewer_user_id` = ' + item.viewer_user_id + ' OR `owner_user_id` = ' + item.viewer_user_id + ') AND (`viewer_user_id` = ' + item.owner_user_id + ' OR `owner_user_id` = ' + item.owner_user_id + ') ORDER BY `mail_id` DESC LIMIT 1';
    var dialogs = [];
    connection.query(sql, function(err, rows) {
        someCallBackArgument(err, rows);
    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

I'm assuming this is a question about how to use async.map: I've rewritten your code so it uses callbacks properly: You will need to adjust the output but this should return you an array with all the data you require.

connection.query(sql, function(err, rows) {
      if (err) throw err;

      async.map(rows, function(item, callback) {
          var sql = 'SELECT `mail_id`, `subject`, `preview`, `owner_user_id`, `viewer_user_id`, `viewer_is_new`, `time_stamp` FROM `phpfox_mail` WHERE (`viewer_user_id` = ' + item.viewer_user_id + ' OR `owner_user_id` = ' + item.viewer_user_id + ') AND (`viewer_user_id` = ' + item.owner_user_id + ' OR `owner_user_id` = ' + item.owner_user_id + ') ORDER BY `mail_id` DESC LIMIT 1';
          var dialogs = [];
          connection.query(sql, function(err, rows) {
            if(err) return callback(err);
            callback(null, { item: item, rows: rows} );
          });

      }, function(err, item) {
          // Here I have to get the generated array with all the necessary dialogue.
          console.log(JSON.stringify(item));

          IM.socket.emit('logger', {text: 'dataIsABuilding', key: 'success'});
          IM.socket.emit('dialogsBuilding', item);
      });
    });

8 Comments

Your answer is most close to the target. As I get a callback object that stores information about the dialogue and the identifier interlocutors. { item: { owner_user_id: 37251, viewer_user_id: 1 }, rows: [ { mail_id: 3123, subject: 'You have received an infraction.', etc... But I need to get information about users whose identities are described in the value of "item".
LOL, it's logically identical to mine... "most close to target"... that's funny. In this case your query could just be messed up a little bit, just double check that your query is correct. Also, the variable rows gets redefined like 40 times in the same scope... at the very least this looks terrible, and combining this with async could be doing something weird.
I started writing my answer first! :) I would recommend factoring out the anymous functions into named functions to clean up the code and bring clarity. (This would help with performance too)
So sue me for trying to get him to type a legible question first! :) LOL, who gets the rep doesn't bother me, it's Roman's comment about "most close to target" suggesting that he read both, and believed that one was distinctly better/or even different than the other, that cracked me up. On the other hand, I think I understand his problem better now...
@ChrisCM, AndyD Guys? :) Please explain to me how to fill an array of previously obtained customer information and I'll leave up to you :)
|
0

Yes! :)

IM = {
    iUserId: false,
    socket: false,
    checkDialog: function(socket) {
        this.socket = socket;
        var sql = 'SELECT DISTINCT(`owner_user_id`), `viewer_user_id` FROM `phpfox_mail` WHERE `owner_user_id` = ' + this.iUserId + ' GROUP BY 1,2 UNION SELECT DISTINCT (`viewer_user_id`), `owner_user_id` FROM `phpfox_mail` WHERE `viewer_user_id` = ' + this.iUserId + ' GROUP BY 1,2 ORDER BY 1 ';
        connection.query(sql, function(err, rows) {
          if (err) throw err;

          async.map(rows, function(item, nextParent) {
              var sql = 'SELECT `mail_id`, `subject`, `preview`, `owner_user_id`, `viewer_user_id`, `viewer_is_new`, `time_stamp` FROM `phpfox_mail` WHERE (`viewer_user_id` = ' + item.viewer_user_id + ' OR `owner_user_id` = ' + item.viewer_user_id + ') AND (`viewer_user_id` = ' + item.owner_user_id + ' OR `owner_user_id` = ' + item.owner_user_id + ') ORDER BY `mail_id` DESC LIMIT 1;';
              var dialogs = [];
            connection.query(sql, function(err, rows) {
              if(err) return nextParent(err);
              nextParent(null, rows[0]);
            });

          }, function(err, item) {             
             async.map(item, function(dialog, next) {
                 connection.query('SELECT `user_name`, `full_name`, `user_profile_image` FROM `phpfox_user` WHERE `user_id` = ' + dialog.owner_user_id, function(err, user) {
                     next(err, {owner: user[0], dialog: dialog});
                 });
             }, function(err, rows) {                 
                 async.map(rows, function(dialog, next) {
                     connection.query('SELECT `user_name`, `full_name`, `user_profile_image` FROM `phpfox_user` WHERE `user_id` = ' + dialog['dialog']['viewer_user_id'], function(err, user) {
                         next(err, {viewer: user[0], dialog: dialog['dialog'], owner: dialog['owner']});
                     });
                 }, function(err, dialogs) {
                     console.log(dialogs);
                     IM.socket.emit('logger', {text: 'dataIsABuilding', key: 'success'});
                     IM.socket.emit('dialogsBuilding', dialogs);
                 });                 
             });
          });
        });
    }

And it worked! :)

Screen http://screencloud.net//img/screenshots/411f98a583916a41142c40294fd2ee00.png

But it seems to me, this code - it sucks!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.