【问题标题】:(TypeError): _aws_amplify_core__WEBPACK_IMPORTED_MODULE_8__.AWS.DynamoDB is not a constructor(类型错误):_aws_amplify_core__WEBPACK_IMPORTED_MODULE_8__.AWS.DynamoDB 不是构造函数
【发布时间】:2019-07-25 05:06:56
【问题描述】:

我正在用 React 编写我的 Web 应用程序。 (不是 React Native

昨天,这是有效的。我尝试过使用import 语句和API 版本。现在我收到以下错误:

未处理的拒绝 (TypeError):_aws_amplify_core__WEBPACK_IMPORTED_MODULE_8__.AWS.DynamoDB 不是构造函数

import React, { Component } from 'react'
import { Route, Switch } from "react-router-dom";
import Amplify, { Auth  } from 'aws-amplify';
import { AWS } from '@aws-amplify/core';
import { CognitoIdentityServiceProvider } from 'aws-sdk';

import PropTypes from 'prop-types'

import '../../stylesheets/Dashboard.css'

export default class Dynamo extends Component {

    constructor(props) {
        super(props)
        this.state = {}
    }

    componentDidMount() {
        Amplify.configure({
            Auth: {
                identityPoolId: identityPoolId,
                region: region,
                userPoolId: userPoolId,
                userPoolWebClientId: userPoolWebClientId,
            },
        });

        var context = this;
        Auth.currentCredentials().then(credentials => {
            // Constructor for the global config.
            var AWSconfig = new AWS.Config({
                apiVersion: '2016-04-18',
                credentials: credentials,
                region: 'us-XXXX-#'
            });
            console.log(credentials)
            var dynamodb = new AWS.DynamoDB({
                region: 'us-XXXX-#',
                credentials: credentials
            }); 
            var params = {
                Item: {
                    "email": {
                        S:`first.last@gmail.com`
                    },
                    "date": {
                        N: `${new Date()}`
                    },
                    "hours_worked": {
                        N: `5`
                    },
                    "note_approved": {
                        BOOL: (Math.random() < 0.5)
                    },
                    "note_written": {
                        BOOL: (Math.random() < 0.5)
                    },
                    "program": {
                        S: `*************************`
                    },
                    "total_miles": {
                        N: `${parseInt(Math.random() * 20 + 1)}`
                    }
                }, 
                ReturnConsumedCapacity: "TOTAL", 
                TableName: "Services"
            };
            dynamodb.putItem(params, function(err, data) {
                if (err) 
                    console.log(err, err.stack); // an error occurred
                else {
                    console.log(data); // successful response
                }
            });
            params = {
                ExpressionAttributeNames:{
                    "#email": "email",
                    "#datetime_from": "date"
                },
                ExpressionAttributeValues: {
                    ":e": {
                        S: "first.last@gmail.com"
                    },
                    ":dearly": {
                        N: "1551500000000"
                    }, 
                    ":dlate": {
                        N: "1551600000000"
                    }
                }, 
                KeyConditionExpression: "#email = :e and #datetime_from BETWEEN :dearly and :dlate", 
                TableName: "Services"
            };
            dynamodb.query(params, function(err, data) {
            if (err) console.log(err, err.stack);   // an error occurred
            else {
                console.log(data);                  // successful response
                context.setState({
                    records: data
                })
            }   
            });
        });
    }

    render() {
        return (
            <div className='Dynamo'>
                I AM RENDERING<br />
                {this.state.records ? JSON.stringify(this.state.records) : null}
            </div>
        )
    }
}

Dynamo.propTypes = {

}

【问题讨论】:

  • 看起来AWS.DynamoDB() 构造函数不包含在@aws-amplify/core 中。

标签: reactjs amazon-web-services amazon-dynamodb amazon-cognito


【解决方案1】:

在@Adam 的评论之后,我看到AWS.DynamoDB 是未定义的。无论如何,我没有使用CognitoIdentityServiceProvider,所以我用DynamoDB 模块替换了它。希望这个 sn-p 对其他类似丢失的 React 用户有用。

import React, { Component } from 'react'
import { Route, Switch } from "react-router-dom";
import Amplify, { Auth  } from 'aws-amplify';
// import { AWS } from '@aws-amplify/core'; Following @Adam's comment. 
import { DynamoDB } from 'aws-sdk';

import PropTypes from 'prop-types'

import '../../stylesheets/Dashboard.css'

export default class Dynamo extends Component {

    constructor(props) {
        super(props)
        this.state = {}
    }

    componentDidMount() {
        Amplify.configure({
            Auth: {
                identityPoolId: identityPoolId,
                region: region,
                userPoolId: userPoolId,
                userPoolWebClientId: userPoolWebClientId,
            },
        });

        var context = this;
        Auth.currentCredentials().then(credentials => {
            // Constructor for the global config.
            console.log(credentials)
            var dynamodb = new DynamoDB({
                region: region,
                credentials: credentials
            }); 
...

【讨论】:

    猜你喜欢
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 2013-09-26
    • 2023-02-16
    • 2019-09-01
    • 2020-02-18
    • 1970-01-01
    相关资源
    最近更新 更多