(TypeError):_aws_amplify_core__WEBPACK_IMPORTED_MODULE_8 __.AWS.DynamoDB不是构造函数 [英] (TypeError): _aws_amplify_core__WEBPACK_IMPORTED_MODULE_8__.AWS.DynamoDB is not a constructor

查看:84
本文介绍了(TypeError):_aws_amplify_core__WEBPACK_IMPORTED_MODULE_8 __.AWS.DynamoDB不是构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用React编写我的Web应用程序.(不响应本机)

I'm programming my web application in React. (Not React Native)

昨天,这个工作正常.我尝试使用 import 语句和API版本.现在出现以下错误:

Yesterday, this was working. I've tried playing around with import statements and API versions. Now I'm getting the following error:

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

Unhandled Rejection (TypeError): _aws_amplify_core__WEBPACK_IMPORTED_MODULE_8__.AWS.DynamoDB is not a constructor

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 = {

}

推荐答案

在@Adam的评论之后,我看到未定义 AWS.DynamoDB .无论如何,我都没有使用 CognitoIdentityServiceProvider ,所以我将其替换为 DynamoDB 模块.希望此代码片段可用于其他同样丢失的React -ers.

Following @Adam's comment, I saw that AWS.DynamoDB is undefined. I wasn't using CognitoIdentityServiceProvider anyhow, so I replaced it with the DynamoDB module. Hopefully this snippet comes in handy for other similarly lost React -ers.

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
            }); 
...

这篇关于(TypeError):_aws_amplify_core__WEBPACK_IMPORTED_MODULE_8 __.AWS.DynamoDB不是构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆