Twilio + NodeJS how to check efficiently if two users have participated in a conversation together

Advertisements

I am trying to query Twilio to see if 2 users have ever engaged in a conversation. I query conversations.user.userConversations to get a list of conversations the primary user has been in, then i filter that list to check where the participantSid of the main user in any of their conversations matches the sid of the participant user.

My function doesnt work, why?

const checkForExistingConversations = async (loggedInUserSid, participantUserSid) => 

{
  let existingConversations = await client.conversations.users(loggedInUserSid)
      .userConversations
      .list()
  existingConversations.filter((convo) =>  convo.participantSid === participantUserSid)  // here is the issue
  return existingConversations
}

>Solution :

The filter method does not mutate the array, but returns a new one, so your function should look like this:

const checkForExistingConversations = async (loggedInUserSid, participantUserSid) => 
{
  let existingConversations = await client.conversations.users(loggedInUserSid)
      .userConversations
      .list()
  return existingConversations.filter((convo) =>  convo.participantSid === participantUserSid)
}

Leave a ReplyCancel reply