在Svelte 5中使用Agora Chat登录时遇Error 28及700问题求助
问题分析与解决方案
首先你的代码存在语法逻辑错误,这是触发报错的直接原因:
await client.open({ user: userId, accessToken: token }) .then(await client.joinChatRoom({roomId: 'smitty_chat'}));
.then()方法需要传入回调函数,而你直接执行了await client.joinChatRoom(...),这会导致joinChatRoom在client.open登录完成前就被调用——此时客户端还未完成身份验证,token自然无法被正确识别,进而触发"token not assign error"。
修正后的代码写法
用链式await保证执行顺序:
const AgoraChat = (await import('agora-chat')).default; client = new AgoraChat.connection({ appKey: appKey }); // 先完成登录,再执行加入聊天室操作 await client.open({ user: userId, accessToken: token }); await client.joinChatRoom({roomId: 'smitty_chat'});
或者使用.then()的正确回调写法:
const AgoraChat = (await import('agora-chat')).default; client = new AgoraChat.connection({ appKey: appKey }); client.open({ user: userId, accessToken: token }) .then(() => client.joinChatRoom({roomId: 'smitty_chat'})) .catch(error => console.error('Chat operation failed:', error));
其他排查方向
如果修正语法后仍报错,检查以下几点:
- 确认
token是针对当前userId和appKey生成的,Agora Chat的token与用户ID、AppKey强绑定,不能混用其他用户或应用的token。 - 检查
appKey格式是否正确,必须是{OrgName}#{AppName}的格式,注意大小写和分隔符#。 - 验证
token是否过期,生成的token有有效期,过期需重新生成。 - 在
client.open前打印appKey、userId、token的值,确认没有空值或undefined。
内容的提问来源于stack exchange,提问作者SingingSmitty
相关产品推荐
相关产品推荐

