错误调用“ UpdateToken”在客户拒绝国家变更上,使用Twilio对话JS SDK

发布于 2025-02-01 23:28:15 字数 895 浏览 3 评论 0 原文

我正在使用Twilio对话JS SDK,如果它过期或无效,我需要刷新连接令牌。问题是,如果我调用 updateToken()函数时,当连接状态更改为“拒绝”时,我会收到以下错误:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'updateToken')

我是这样的:

let conversationsClient = new Twilio.Conversations.Client('SOME_EXPIRED_OR_INVALID_TOKEN')
conversationsClient.on("connectionStateChanged", (state) => {
  switch (state) {
    case "denied":
      conversationsClient.updateToken(getConversationsToken());
      break
  }
});

出于某种原因,我的>对话clionclient 对象进入拒绝状态时,它是不确定的。

根据SDK文档(),我可以在更改状态更改以否认状态时更新令牌。

I am using the Twilio conversations JS SDK, and I need to refresh my connection token if it is expired or invalid. The issue is that, if I call the updateToken() function when the connection state change to "denied", I get the following error:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'updateToken')

I am using it like this:

let conversationsClient = new Twilio.Conversations.Client('SOME_EXPIRED_OR_INVALID_TOKEN')
conversationsClient.on("connectionStateChanged", (state) => {
  switch (state) {
    case "denied":
      conversationsClient.updateToken(getConversationsToken());
      break
  }
});

For some reason my conversationsClient object is undefined when it enters the denied state.

According the SDK documentation (https://media.twiliocdn.com/sdk/js/conversations/releases/2.0.0/docs/modules.html#ConnectionState), I can update the token when the state is changed to denied.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江挽川 2025-02-08 23:28:15

我的猜测是您的 getConversationStoken 函数是异步的,因为它可能向您的服务器提出了请求。但是您正在传递该功能的结果,而不是异步作用的结果。

我假设 getConversationStoken 返回承诺,因此您可以通过在然后在 callback中更新令牌来解决此问题:

let conversationsClient = new Twilio.Conversations.Client('SOME_EXPIRED_OR_INVALID_TOKEN')
conversationsClient.on("connectionStateChanged", (state) => {
  switch (state) {
    case "denied":
      getConversationsToken().then(token => {
        conversationsClient.updateToken(token);
      }).catch(error => console.error(error));
      break
  }
});

或者,您可以使用 async/等待这样:

let conversationsClient = new Twilio.Conversations.Client('SOME_EXPIRED_OR_INVALID_TOKEN')
conversationsClient.on("connectionStateChanged", async (state) => {
  switch (state) {
    case "denied":
      conversationsClient.updateToken(await getConversationsToken());
      break
  }
});

My guess here is that your getConversationsToken function is asynchronous as it presumably makes a request to your server. But you are passing the result of the function, not the result of the asynchronous action.

I assume that getConversationsToken returns a promise, so you might be able to fix this by updating the token in the then callback:

let conversationsClient = new Twilio.Conversations.Client('SOME_EXPIRED_OR_INVALID_TOKEN')
conversationsClient.on("connectionStateChanged", (state) => {
  switch (state) {
    case "denied":
      getConversationsToken().then(token => {
        conversationsClient.updateToken(token);
      }).catch(error => console.error(error));
      break
  }
});

Or, you could use async/await like this:

let conversationsClient = new Twilio.Conversations.Client('SOME_EXPIRED_OR_INVALID_TOKEN')
conversationsClient.on("connectionStateChanged", async (state) => {
  switch (state) {
    case "denied":
      conversationsClient.updateToken(await getConversationsToken());
      break
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文