0

I am a new javascript user and I am looking for a rescuer the result (count) of a memory request.

More precisely, maybe you can comment, put my variable in my conv object

right now my result is [promised object] I tried with async and promised javascript but I do not understand the logic yet

Thank you so much,

Gabriel

                            var conversations = []
                            for (var i = 0; i < conversation.length; i++) {
                                for (var j = 0; j < conversation[i].users.length; j++) {
                                    conv = conversation[i].users[j]                 


                                    async function asyncCall() {
                                        function countUnread() {
                                            filterUnread = {$and:[
                                                {toID: user.properties.chat.id}, 
                                                {fromID: conversation[i].users[j].id},
                                                { read: false }]}
                                            return new Promise(resolve => {
                                                Message.countDocuments(filterUnread, function (err, count) {
                                                    resolve(count)
                                                })
                                            })
                                        }

                                        var count = await countUnread();
                                        console.log(count);
                                        console.log(conv)
                                        resolve(!!!!count!!!!) ;

                                    }
                                    asyncCall();
                                    conv.unread = !!!!I_want_my_count_here!!!!
                                    conversations.push(conv);
                                    resolve(conversations)
                                }
                            }

1 Answers1

0

Mixing async functions and Promise constructors makes the code hard to track and causes problems like this one. Instead only wrap the most low level parts into Promise constructors, let tuem resolve to something useful, then compose multiple such Promises using async :

  const filter = { users: { $elemMatch: { id: user.properties.chat.id } } }

function getConversations(filter) {
  return new Promise(function(resolve, reject) {
     Conversation.find(filter, function(err, conversations) {
        if(err) reject(err) else resolve(conversations);
     });
  });
}

function countUnread(user) {
   const filterUnread = { $and:[
       {toID: user.properties.chat.id}, 
       {fromID: user.id},
       { read: false }
   ]};

   return new Promise((resolve, reject) => {
       Message.countDocuments(filterUnread, function (err, count) {
           if(err) reject(err) else resolve(count);
       });
   });
}


async function composeConversations() {
  const conversations = await getConversations();
  const result = [];

  for(const conversation of conversations) {
    for(const user of conversation.users) {
       const count = await countUnread(user);
       user.unread = count;
    }
    result.push(conversation);
  }
  return result;
}
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • 1
    The way I read the question, `conv.unread` should be the sum total of all the unread chats(?) across all users in that conversation, ie, initialise `user.unread = 0`, then `user.unread += count;` in the inner loop. – Roamer-1888 Dec 21 '18 at 17:52
  • 1
    Does `result.push(conversation)` not just compose an array identical to `conversations`? – Roamer-1888 Dec 21 '18 at 17:54