Uncaught TypeError: Cannot read property 'getMessage' of undefined

Example code provided at Open channel | Chat JavaScript SDK | Sendbird Docs (shown below) fails with “Uncaught TypeError: Cannot read property ‘getMessage’ of undefined”

// Create a `MessageRetrievalParams` object.
const params = new sb.MessageRetrievalParams();
params.messageId = MESSAGE_ID;
params.channelType = CHANNEL_TYPE;
params.channelUrl = CHANNEL_URL;
...

// Pass the params as an argument to the parameter in the `getMessage()` method.
sb.BaseMessage.getMessage(params, (message, error) => {
    if (error) {
        // Handle error.
    }

    // The specified message is successfully retrieved.
    ...
});

Hi @rewinddev,

It looks like our documentation needs to be updated. It should be sb.UserMessage.getMessage(). Here is a codesandbox showing it in action:

I had already tried that. Here’s is the same error message for that code in action:

“Uncaught TypeError: sendBird.UserMesssage is undefined”

We are using:

“Sendbird JavaScript SDK v3.0.132”

and your code in action yields:

“Uncaught SyntaxError: await is only valid in async functions, async generators and modules”

Can you please provide a working example of what your current posted JavaScript API reference provides, to include passing a call back as the second parameter? Your documentation seems invalid not only in your online examples but also your JavaScript reference documentation.

[Edit]
Here’s the current code (now using async), which yields “Invalid Parameter”:

async function deleteMsg(messageId) {
const params = new sendBird.MessageRetrievalParams();
params.messageId = messageId;
params.channelType = currentChannel.channelType;
params.channelUrl = currentChannel.channelUrl;
const message = await sendBird.UserMessage.getMessage(params);
console.error(message);
}

In your async update, it looks like it should be params.channelUrl = currentChannel.url if current channel is an unaltered channel object.

Edit: I should note, you’re currently using an SDK version that is over 9 months old. You may want to consider updating to a more current version.

Thanks for the catch/correction. I can now get a message object, but now the delete call is failing. I just added this code (shown below, also per your Guide and API ref documentation), and it yields “Uncaught TypeError: r is null”. I get the same error if I try using an async call.

currentChannel.deleteMessage(message, err => {
        console.error('Failed to delete message or linked message:' + messageId + '::' + err);
    });

[Edit]
Here’s the full/latest code yield that same error:

async function deleteMsg(messageId) {
    const params = new sendBird.MessageRetrievalParams();
    params.messageId = messageId;
    params.channelType = currentChannel.channelType;
    params.channelUrl  = currentChannel.url;
    const message = await sendBird.UserMessage.getMessage(params);
    console.error(message);

    await currentChannel.deleteMessage(message);
}

[Edit 2]
Here’s the link to your documentation that I was following (it doesn’t use the async call):

[Edit 3]
…and here’s the link to your JavaScript API reference documentation:
https://sendbird.github.io/core-sdk-javascript/module-model_baseChannel-BaseChannel.html#deleteMessage

Here is a new example for you, utilizing callbacks. I created a form where you can put in your information and delete a message in your own environment.

Thank you for your example code, however I just refactored my code to use your async example (because it now can at least retrieve a message), and your response does not address the error I’m now getting using your previous example code, which is now the 4th iteration of example code that has failed. Also, I’m not comfortable putting in sensitive information on an unknown/untrusted domain (which really seems like an unusual suggestion to do so) - Now that I’ve refactored, can you please tell me:

1-why the following code fails with “Uncaught TypeError: r is null”
2-how to fix/change/edit the code to actually work

async function deleteMsg(messageId) {
const params = new sendBird.MessageRetrievalParams();
params.messageId = messageId;
params.channelType = currentChannel.channelType;
params.channelUrl = currentChannel.url;
const message = await sendBird.UserMessage.getMessage(params);
console.error(message);

await currentChannel.deleteMessage(message);

}

This appears to be an issue with the SDK. At this time, we believe its best that you update to the latest version and test again. Using the code you’ve provided, I don’t have any issues utilizing this function. The SDK you’re on is from August 21, 2020.

I’ve tested the code you provided in 3.0.151 without issue.

OK, thanks, I’ll try using the latest version from github (again) - the first time I tried I also got an error but may not have been the same one (i think that’s the one you referenced)

I just updated SDK to: Sendbird JavaScript SDK v3.0.151

I then got synch manager errors, so I updated SyncManager to: SendBird SyncManager JavaScript SDK v1.1.26

I’m now getting dozens of error stack traces in the console (see below), BUT delete is now working.

Please advice on how to fix this latest error(s):

Uncaught (in promise) SendBirdException: Command received no ack. m h…/src/libs/SendBird.min.js:6
r …src/libs/SendBird.min.js:6
U …src/libs/SendBird.min.js:6
t …src/libs/SendBird.min.js:6
timer …src/libs/SendBird.min.js:6
SendBird.min.js:6:2963

I see that you submitted a case for this as well. Lets continue to work via the case moving forward.