1

I am calling the following method on endpoint

exports.allConversations = async (req, res) => {
    // The recipient of the call, a phone number or a client
    console.log(req.body);
    let phoneNumber = req.body.phoneNumber;
    let messageSent, messageReceived;
    try {
        let messageSentPromise = client.messages
            .list({
                from: phoneNumber,
                limit: 20
            });
        let messageReceivedPromise = client.messages
            .list({
                to: phoneNumber,
                limit: 20
            });
        [messageSent, messageReceived] = await Promise.all([messageSentPromise, messageReceivedPromise]);

        let conversations = [];
        for (let i = 0; i < messageSent.length; i++) {
            let message = messageSent[i];
            if (conversations.includes(message.to))
                conversations[message.to].push(message);
            else {
                console.log([].push(message));
                conversations[message.to] = [message];
            }
        }
        console.log(conversations);
        return res.status(200).send({
            conversations: conversations
        });
    }
    catch (error) {
        console.log(error);
        return res.status(200).json({
            success: false
        });
    }
}

console.log(conversations); In the response I am always receiving an empty array but the console.log above the following code

return res.status(200).send({
                conversations: conversations
            });

prints the array on console.

Rafay Hassan
  • 740
  • 8
  • 23
  • 1
    maybe try using `res.json({conversations})` ? it might be an issue with your object being sent without appropriate headers – Krzysztof Krzeszewski Oct 21 '19 at 13:37
  • @KrzysztofKrzeszewski have tried res.json() and still the same issue. – Rafay Hassan Oct 21 '19 at 13:41
  • See [here](https://stackoverflow.com/questions/19696240/proper-way-to-return-json-using-node-or-express). Try to set the headers and stringify your response before sending. – Mickers Oct 21 '19 at 13:41
  • btw what are you trying to achieve in here `if (conversations.includes(message.to))` it seems like you are checking for existence of the element `message.to` on the array, however from what i can tell `message.to` is not an element but the index of the element `conversations[message.to] = [message];` – Krzysztof Krzeszewski Oct 21 '19 at 13:45
  • @Mickers it doesn't make sense to stringify the response but I have tried it and it still doesn't work. – Rafay Hassan Oct 21 '19 at 13:45
  • btw are you sure the `console.log` you mentioned is actually logging its answer and you didn't confuse it with `console.log([].push(message));` 3 lines higher? – Krzysztof Krzeszewski Oct 21 '19 at 13:46
  • @KrzysztofKrzeszewski sure that's not the issue and I don't think it can be the cause of the issue. – Rafay Hassan Oct 21 '19 at 13:47
  • Just a shot in the dark but what if you remove the {} and just pass conversations? – Mickers Oct 21 '19 at 13:48
  • @KrzysztofKrzeszewski Nope, I didn't confuse it with console.log([].push(message)) – Rafay Hassan Oct 21 '19 at 13:49
  • @Mickers no, it doesn't help – Rafay Hassan Oct 21 '19 at 13:50
  • can you provide a simplified code to reproduce the issue? right now, everything seems fine to me, assuming that it actually logs the output one line above. Are you even sure the issue lays in this part of the code and not one retrieving the data from provided endpoint? – Krzysztof Krzeszewski Oct 21 '19 at 13:50
  • @KrzysztofKrzeszewski it is the issue with conversations if I send messageSent in the response, I receive the data in the response but something is not right with conversations. – Rafay Hassan Oct 21 '19 at 13:54

1 Answers1

1

I think the issue may be caused by the array indexes not being actual positive integer numbers. Which can cause unusual behavior as demonstrated in the snippet below:

let result = [];
result["test"]="test";
console.log(result); // depending on the implementation can still log `[test: "test"]` works in newest version of nodejs and firefox for instance
console.log(JSON.stringify(result)); // will always log `[]`

Problem may be way less apparent like you using floating point numbers, just ensure your indexes are actually valid array indexes or use an object instead like in the code below

let conversations = {};
for (let i = 0; i < messageSent.length; i++) {
    let message = messageSent[i];
    if (conversations[message.to]) conversations[message.to].push(message);
    else conversations[message.to] = [message];
}
console.log(conversations);
return res.status(200).json({conversations});
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Krzysztof Krzeszewski
  • 5,912
  • 2
  • 17
  • 30