微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Twilio JS - 对话客户端在“fetchConversations”上引发错误

如何解决Twilio JS - 对话客户端在“fetchConversations”上引发错误

我从事 Node、Next.js、React、Redux Web 应用程序的工作,我们一直在使用 Twilio Programmable Chat 来处理我们的聊天服务。自从收到一封建议迁移到 Twilio Conversations API 的电子邮件后,我一直在尝试迁移到 Twilio Conversations API,但在尝试初始化 Conversations 客户端时遇到了错误

看起来客户端已经初始化,但它立即抛出一个错误,说明如下:

Uncaught (in promise) TypeError: Cannot create property 'dateUpdated' on string '{"current":"ACTIVE","dateUpdated":"2020-04-18T03:07:14.977Z"}'
    at Conversation._update (conversation.js?e3a6:482)
    at eval (conversation.js?e3a6:261)
    at async Promise.all (index 0)
_update @   conversation.js?e3a6:482
eval    @   conversation.js?e3a6:261
Promise.catch (async)       
fetchConversations  @   conversations.js?dc06:264
eval    @   client.js?ece4:266
Promise.then (async)        
Client  @   client.js?ece4:246
_callee10$  @   client.js?ece4:730
tryCatch    @   runtime.js?96cf:62
invoke  @   runtime.js?96cf:296
prototype.<computed>    @   runtime.js?96cf:114
step    @   asyncToGenerator.js?0f75:17
eval    @   asyncToGenerator.js?0f75:35
F   @   _export.js?63b6:36
eval    @   asyncToGenerator.js?0f75:14
create  @   client.js?ece4:750
descriptor.value    @   typecheck.js?92de:187
_callee4$   @   twilioSession.js?cf74:63
tryCatch    @   runtime.js?98b8:63
invoke  @   runtime.js?98b8:293
eval    @   runtime.js?98b8:118
asyncGeneratorStep  @   asyncToGenerator.js?1da1:3
_next   @   asyncToGenerator.js?1da1:25
Promise.then (async)        
asyncGeneratorStep  @   asyncToGenerator.js?1da1:13
_next   @   asyncToGenerator.js?1da1:25
eval    @   asyncToGenerator.js?1da1:32
eval    @   asyncToGenerator.js?1da1:21
test    @   twilioSession.js?cf74:60
onClick @   VM5428 SeekerHeader.jsx:616
handleClick @   button.js?e019:226
callCallback    @   react-dom.development.js?61bb:188
invokeGuardedCallbackDev    @   react-dom.development.js?61bb:237
invokeGuardedCallback   @   react-dom.development.js?61bb:292
invokeGuardedCallbackAndCatchFirstError @   react-dom.development.js?61bb:306
executedispatch @   react-dom.development.js?61bb:389
executedispatchesInorder    @   react-dom.development.js?61bb:414
executedispatchesAndRelease @   react-dom.development.js?61bb:3278
executedispatchesAndReleasetopLevel @   react-dom.development.js?61bb:3287
forEachAccumulated  @   react-dom.development.js?61bb:3259
runEventsInBatch    @   react-dom.development.js?61bb:3304
runExtractedpluginEventsInBatch @   react-dom.development.js?61bb:3514
handletopLevel  @   react-dom.development.js?61bb:3558
batchedEventUpdates$1   @   react-dom.development.js?61bb:21871
batchedEventUpdates @   react-dom.development.js?61bb:795
dispatchEventForLegacyPluginEventSystem @   react-dom.development.js?61bb:3568
attemptTodispatchEvent  @   react-dom.development.js?61bb:4267
dispatchEvent   @   react-dom.development.js?61bb:4189
unstable_runWithPriority    @   scheduler.development.js?3069:653
runWithPriority$1   @   react-dom.development.js?61bb:11039
discreteUpdates$1   @   react-dom.development.js?61bb:21887
discreteUpdates @   react-dom.development.js?61bb:806
dispatchdiscreteEvent   @   react-dom.development.js?61bb:4168

这是客户端初始化代码

import { Client as ConversationsClient } from "@twilio/conversations";

let twilioClient;

const setTwilioClient = async () => {
  try {
    const token = await fetchTokenForTwilioClient();
    twilioClient = await ConversationsClient.create(token);
    if (twilioClient) {
      console.log('twilioClient lives: ',twilioClient);
      twilioClient.on('tokenAboutToExpire',async () => {
        const renewedToken = await fetchTokenForTwilioClient();
        twilioClient.updatetoken(renewedToken);
      });
    }
  } catch (err) {
    if (err) console.log(err);
  }
};

客户端应为其初始化的令牌的服务器端代码

const Accesstoken = require('twilio').jwt.Accesstoken;
const ChatGrant = Accesstoken.ChatGrant;

var twilioGetChatToken = async (req,res) => {
  const {identity} = req.body;
  const chatGrant = new ChatGrant({
    serviceSid: conversationsServiceSid,pushCredentialSid: pushCredentialSid,});

  const token = new Accesstoken(twilioAccountSid,twilioApiKey,twilioApiSecret)

  token.addGrant(chatGrant);
  token.identity = identity;
  res.send(token.toJwt());
}

我尝试了不同的安装和运行 Twilio 和 @twilio/conversations 的不同版本,但这没有帮助。每次我尝试不同的版本时,我都会删除 node_modules 和 yarn.lock/package-lock.json 并在全新安装后启动应用程序,但这还没有解决问题。 我的 Twilio 当前版本是 3.55.0,@twilio/conversations 是 1.1.0。

我已经检查了认的消息传递集成设置,它们似乎也正确。不知道发生了什么。

如果有人能让我更好地了解如何解决错误,我将不胜感激。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。