Javascript SDK 2.15 chatWithGroup not working

for web javascript, CometChatWidget.chatWithGroup(p13) e.g. when GUID is “p13” keeps giving me error: Uncaught ReferenceError: p13 is not defined…am I doing something wrong?

Hello @Arjuna ,

There is a syntactical error in your code. Please try using the below code snippet.

CometChatWidget.chatWithGroup("p13");

Warm Regards,

CometChat Pro
Mayur Bhandari

When I try this, I get the following error:

Uncaught SyntaxError: expected expression, got ‘}’

Hi @Arjuna
Could you please share your code snippet?

messagesRequest.fetchPrevious().then(
messages => {
$("#unreadmessages").empty();
for (let i = 0; i < messages.length; ++i) {
senderId=messages[i].sender.uid;
sendername=messages[i].sender.name;
text=messages[i].text;
type=messages[i].type;
receiverType=messages[i].receiverType;
receiverId=messages[i].receiverId;
receivername = messages[i].receiver.name;
message=messages[i].message;
if (type==“text” && receiverType==“user”)
{
$(’#unreadmessages’).append(’

  • ’ + sendername + ‘: ’ + text + ‘
  • ’);
    }
    if (type==“text” && receiverType==“group”)
    {
    $(’#unreadmessages’).append(’
  • <a href="#" onclick=“javascript:CometChatWidget.chatWithGroup(”’ + receiverId +’"); CometChatWidget.openOrCloseChat(true);"> ’ + receivername + ‘:
    ’ + sendername + ‘: ’ + text + ‘
  • ’);
    }
    if (type==“groupMember”)
    {
    $(’#unreadmessages’).append(’
  • ’ + message + ‘
  • ’);
    }
    }
    unreadmessagesnumber = messages.length;
    if (messages.length)
    {
    $(’#messagesicon’).addClass(‘icon-animated-vertical’);
    }
    $("#unreadmessagesnumber").empty();
    $("#unreadmessagesnumber").append(unreadmessagesnumber);
    },
    error => {
    console.log(“Message fetching failed with error:”, error);
    }
    );

    Can you try removing the extra quote. Please refer the following code:
    $(’#unreadmessages’).append(’* <a href="#" onclick=“javascript:CometChatWidget.chatWithGroup(” + receiverId +"); CometChatWidget.openOrCloseChat(true);"> ’ + receivername + ‘:
    ’ + sendername + ‘: ’ + text + ‘
    ’);

    The extra quote was advised by Mayur in the first reply, remote this extra quote gives the original error:
    (e.g. where GUID is p13)

    Uncaught ReferenceError: p13 is not defined

    Could you try adding this code:
    $(’#unreadmessages’).append(’* <a href="#" onclick=“javascript:CometChatWidget.chatWithGroup(’” + receiverId +"’); CometChatWidget.openOrCloseChat(true);"> ’ + receivername + ‘:
    ’ + sendername + ‘: ’ + text + ‘
    ’);

    That gives syntax error: Uncaught SyntaxError: missing ) after argument list

    Hello Arjuna,

    Can you please share the code snippet inside code blocks instead of normal text because the ticks get changed when pasted as plain text. Please paste the code snippet between two triple backticks. Also please try using the below code snippet.

    messagesRequest.fetchPrevious().then(
        messages => {
            $("#unreadmessages").empty();
            for (let i = 0; i < messages.length; ++i) {
                senderId=messages[i].sender.uid;
                sendername=messages[i].sender.name;
                text=messages[i].text;
                type=messages[i].type;
                receiverType=messages[i].receiverType;
                receiverId=messages[i].receiverId;
                receivername = messages[i].receiver.name;
                message=messages[i].message;
                if (type=="text" && receiverType=="user") {
                    $('#unreadmessages').append('' + sendername + ': ' + text + '');
                }
                if (type=="text" && receiverType=="group") {
                    $('#unreadmessages').append('<a href="#" onclick="javascript:CometChatWidget.chatWithGroup("' + receiverId +'"); CometChatWidget.openOrCloseChat(true);"> ' + receivername + ':' + sendername + ': ' + text + '');
                }
                if (type=="groupMember") {
                    $('#unreadmessages').append('' + message + '');
                }
            }
            unreadmessagesnumber = messages.length;
            if (messages.length) {
                $('#messagesicon').addClass('icon-animated-vertical');
            }
            $("#unreadmessagesnumber").empty();
            $("#unreadmessagesnumber").append(unreadmessagesnumber);
        },
        error => {
            console.log("Message fetching failed with error:", error);
        }
    );
    

    Warm Regards,

    CometChat Pro
    Mayur Bhandari

    will do, thanks for the tip, however above code is still giving this error:

    Uncaught SyntaxError: expected expression, got ‘}’

    Hi @Arjuna,
    There is an issue with the quote… Could you please try this…

        messagesRequest.fetchPrevious().then(
            messages => {
                $("#unreadmessages").empty();
                for (let i = 0; i < messages.length; ++i) {
                    senderId=messages[i].sender.uid;
                    sendername=messages[i].sender.name;
                    text=messages[i].text;
                    type=messages[i].type;
                    receiverType=messages[i].receiverType;
                    receiverId=messages[i].receiverId;
                    receivername = messages[i].receiver.name;
                    message=messages[i].message;
                    if (type=="text" && receiverType=="user") {
                        $('#unreadmessages').append('' + sendername + ': ' + text + '');
                    }
                    if (type=="text" && receiverType=="group") {
                        $('#unreadmessages').append('<a href="#" onclick="javascript:CometChatWidget.chatWithGroup(' + receiverId +'); CometChatWidget.openOrCloseChat(true);"> ' + receivername + ':' + sendername + ': ' + text + '');
                    }
                    if (type=="groupMember") {
                        $('#unreadmessages').append('' + message + '');
                    }
                }
                unreadmessagesnumber = messages.length;
                if (messages.length) {
                    $('#messagesicon').addClass('icon-animated-vertical');
                }
                $("#unreadmessagesnumber").empty();
                $("#unreadmessagesnumber").append(unreadmessagesnumber);
            },
            error => {
                console.log("Message fetching failed with error:", error);
            }
        );

    tried this, giving error:

    Uncaught ReferenceError: p13 is not defined

    Hello @Arjuna,

    This is because a single/double quote is missing. Can you please try the below code snippet?

    if (type=="text" && receiverType=="group") {
        $('#unreadmessages').append('<a href="#" onclick="javascript:CometChatWidget.chatWithGroup(\''+ receiverid +'\'); CometChatWidget.openOrCloseChat(true);"> ' + receivername + ':' + sendername + ': ' + text + '');
    }
    

    Warm Regards,

    CometChat Pro
    Mayur Bhandari

    perfect, that worked! - strange how chatWithUser works fine without adding these extra quotes and backslashes.

    1 Like

    This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.