React Native使用AWS CognitoIdentityProvider遇ReadableStream缺失错误
React Native调用Cognito Provider登录时出现ReadableStream相关错误
React Native中调用Cognito Identity Provider实现登录时,遭遇ReferenceError: Property 'ReadableStream' doesn't exist错误,尝试多种polyfill方案后仍未解决,具体情况如下:
原项目环境与代码
原项目依赖版本:
react-native: "0.69.3"@aws-sdk/client-cloudwatch-logs: "^3.614.0"
登录实现代码:
import 'react-native-url-polyfill/auto'; import 'react-native-get-random-values'; const client = new AWS.CognitoIdentityProvider({ region: EnvConfig.REGION }) const getTokens = async ({username, password}) => { try { setisLoading(true); const {AuthenticationResult} = await client.initiateAuth({ ClientId: EnvConfig.COGNITO_CLIENT_ID, AuthFlow: 'USER_PASSWORD_AUTH', AuthParameters: { USERNAME: username, PASSWORD: password, }, }); console.log({ access_token: AuthenticationResult.AccessToken, id_token: AuthenticationResult.IdToken, refresh_token: AuthenticationResult.RefreshToken, }); setisLoading(false); } catch (error) { console.log(error) setisLoading(false); ToastMessage(error.message); } };
新建项目测试情况
怀疑是依赖版本问题,新建React Native项目,package.json依赖如下:
{ "dependencies": { "@aws-sdk/client-cloudwatch-logs": "^3.614.0", "@aws-sdk/client-cognito-identity-provider": "^3.614.0", "react": "18.2.0", "react-native": "0.74.3", "react-native-get-random-values": "^1.11.0", "react-native-url-polyfill": "^2.0.0", "web-streams-polyfill": "^3.3.3" } }
实现相同登录逻辑后,仍出现相同错误:ReferenceError: Property 'ReadableStream' doesn't exist
已尝试的解决方案及新问题
安装
web-streams-polyfill@4.0.0并添加全局polyfill:import {ReadableStream} from 'web-streams-polyfill' globalThis.ReadableStream = ReadableStream;出现新错误:
TypeError: null is not a function使用
web-streams-polyfill@3.3.3的ponyfill:import ReadableStream from 'web-streams-polyfill/ponyfill'; globalThis.ReadableStream = ReadableStream;出现新错误:
TypeError: right operand of 'instanceof' is not callable
目前问题仍未解决,寻求可行的解决方案。
内容的提问来源于stack exchange,提问作者Abrar Hasan
相关产品推荐
相关产品推荐

