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.

12
  • 1
    maybe try using res.json({conversations}) ? it might be an issue with your object being sent without appropriate headers Commented Oct 21, 2019 at 13:37
  • @KrzysztofKrzeszewski have tried res.json() and still the same issue. Commented Oct 21, 2019 at 13:41
  • See here. Try to set the headers and stringify your response before sending. Commented Oct 21, 2019 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]; Commented Oct 21, 2019 at 13:45
  • @Mickers it doesn't make sense to stringify the response but I have tried it and it still doesn't work. Commented Oct 21, 2019 at 13:45

1 Answer 1

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});
Sign up to request clarification or add additional context in comments.

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.