1

Okay, I have this code containing a nested query inside a for loop

var query = records.find({$or:[{starter:data},{receiver:data}]},{});//check the records table for all persons the logged in user has spoken to
    query.sort('-createDate').exec(function (err, docs){
        if(err) throw err;

        for(var i=docs.length-1; i>= 0; i--)
        {

           var starter  = docs[i].starter;
            var receiver = docs[i].receiver;
            var lasttxt = docs[i].lastMessage; 

            if (starter == socket.usernames){
              var target = receiver;
            }else
            {
              var target = starter;
            }

          usersrec.find({username:target},{}).lean().exec(function (errx, docx){
                if(errx) throw errx;

                docx[0].message = lasttxt;
                socket.emit('usernames', docx);
          });
        }
    })

Its meant to get the last message of each person the currently logged in user has spoken to and store in the lasttxt variable. Problem is it only gets the last message of the last user in the database It then then assigns this last message to everyone as their own last msg.

This doesn't affect the database's record. just the client side What am i missing?

Willower
  • 1,099
  • 8
  • 22
  • Check out these questions http://stackoverflow.com/questions/21829789/node-mongoose-find-query-in-loop-not-working and http://stackoverflow.com/questions/31304629/mongoose-find-call-inside-for-loop-using-a-latch – chridam Oct 19 '16 at 12:36
  • If i perform a console.log(lasttxt) right before the `usersrec.find` query, it returns the right results., But the moment i place the `lasttxt` variable inside the query, it only gets the last occurrence of that field from the db – Willower Oct 19 '16 at 12:37
  • Okay, @chridam im heading there now – Willower Oct 19 '16 at 12:38
  • 1
    @chridam async is a `female dog` . I played around with sockets by emitting the needed data to and fro the client and server. And voila! it worked – Willower Oct 19 '16 at 13:08
  • Glad you sorted this. You can post your solution here for the benefit of others who may bump into the same predicament. – chridam Oct 19 '16 at 13:10
  • Oh okay. id do that now – Willower Oct 19 '16 at 13:11

1 Answers1

1

To navigate the js async, I did some to and fro emitting with socket.io and it worked

on the server side

var query = records.find({$or:[{starter:data},{receiver:data}]},{});//check the records table for all persons the logged in user has spoken to
query.sort('-createDate').exec(function (err, docs){
    if(err) throw err;

    for(var i=docs.length-1; i>= 0; i--)
    {

       var starter  = docs[i].starter;
        var receiver = docs[i].receiver;
        var lasttxt = docs[i].lastMessage; 

        if (starter == socket.usernames){
          var target = receiver;
        }else
        {
          var target = starter;
        }

      var userlast = target+" "+lasttxt;
                socket.emit('lastly', userlast);//Emit the username and last message for the client to emit back here
    }
})

On your client side, Pick up the emitted data

 socket.on('lastly', function(data){//Recieve the data and send right back
                  socket.emit('lastly2', data);
              });

Back on you server side, pick up the data sent back

socket.on('lastly2', function(data){//receive the username and last message to work with

var check = data;
var space = check.indexOf(' ');
var name = check.substr(0, space);
var msg = check.substr(space+1);

usersrec.find({username:name},{}).lean().exec(function (errx, docx){
            if(errx) throw errx;

            docx[0].message = msg;
            socket.emit('usernames', docx);
      });

Yeah its probably unorthodox, but at least it gets the job done. Im open to better suggestion tho

Willower
  • 1,099
  • 8
  • 22