aws-放大PubSub.publish TypeError:无法读取未定义的属性'byteLength'

问题描述 投票:0回答:1

我正在尝试访问aws-amplify PubSub.publish以发布到AWS IoT Core中的主题。我正在使用版本"aws-amplify": "2.1.0"

handleSubmit = async () => {
    await PubSub.publish('topic', { msg: 'Hello to all subscribers!' });
};

我的aws-exports.js文件配置如下。

const awsmobile = {
    "aws_project_region": "us-east-2",
    "aws_cognito_region": "us-east-2",
    "aws_user_pools_id": "poolid",
    "aws_user_pools_web_client_id": "webclientid",
    "aws_cognito_identity_pool_id": "identitypoolid",
    "oauth": {}
};

export default awsmobile;

单击按钮会调用handleSubmit功能,并且PubSub.publish会出现以下错误。

Uncaught (in promise) TypeError: Cannot read property 'byteLength' of undefined
    at Object.isEmptyData (browserHashUtils.js:30)
    at Hmac.push../node_modules/aws-sdk/lib/browserHmac.js.Hmac.update (browserHmac.js:34)
    at encrypt (Signer.js:50)
    at get_signing_key (Signer.js:222)
    at Function.Signer.signUrl (Signer.js:374)
    at AWSIoTProvider.<anonymous> (AWSIotProvider.js:233)
    at step (AWSIotProvider.js:152)
    at Object.next (AWSIotProvider.js:83)
    at fulfilled (AWSIotProvider.js:37)
amazon-web-services amazon-cognito publish-subscribe aws-amplify aws-iot
1个回答
0
投票

由于未正确配置Amplify PubSub Provider而发生。

我的错误配置:

Amplify.addPluggable(new AWSIoTProvider({
  aws_pubsub_region: process.env.region,
  aws_pubsub_endpoint: `wss://${process.env.REACT_APP_MQTT_ID}.iot.${process.env.REACT_APP_REGION}.amazonaws.com/mqtt`,
}));

这里我还没有设置process.env.region env变量。这导致了错误。将其更改为适当的env变量(process.env.REACT_APP_REGION)可解决此问题。

工作配置:

Amplify.addPluggable(new AWSIoTProvider({
  aws_pubsub_region: process.env.REACT_APP_REGION,
  aws_pubsub_endpoint: `wss://${process.env.REACT_APP_MQTT_ID}.iot.${process.env.REACT_APP_REGION}.amazonaws.com/mqtt`,
}));
© www.soinside.com 2019 - 2024. All rights reserved.