代码之家  ›  专栏  ›  技术社区  ›  Vingtoft

使用DynamoDB会导致“错误类型错误:无法将未定义或空转换为对象”

  •  0
  • Vingtoft  · 技术社区  · 6 年前

    请考虑以下代码:

    import * as AWS from 'aws-sdk';
    import {Component, OnInit} from '@angular/core';
    
    declare let AWSCognito: any;
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    
    export class AppComponent implements OnInit {
    
      private _dynamodb;
    
      constructor() {
        AWS.config.region = 'eu-west-2'; // Region
        AWS.config.credentials = new AWS.CognitoIdentityCredentials({
          IdentityPoolId: 'eu-west-2:xxyyzz'
        });
        this._dynamodb = new AWS.DynamoDB.DocumentClient();
      }
    
      ngOnInit() {
        this._dynamodb.get({TableName: 'somename', Key: {projectId: 123321}}, (err, data) => {
        })
      }
    }
    

    执行上述代码时,将出现以下错误:

    ERROR TypeError: Cannot convert undefined or null to object
    

    this._dynamodb.get 这会导致系统崩溃。

    知道我做错了什么吗?

    完整堆栈跟踪:

    ERROR TypeError: Cannot convert undefined or null to object
        at hasOwnProperty (<anonymous>)
        at isEndpointDiscoveryApplicable (discover_endpoint.js:279)
        at Request.discoverEndpoint (discover_endpoint.js:322)
        at Request.callListeners (sequential_executor.js:102)
        at Request.emit (sequential_executor.js:78)
        at Request.emit (request.js:683)
        at Request.transition (request.js:22)
        at AcceptorStateMachine.runTo (state_machine.js:14)
        at state_machine.js:26
        at Request.<anonymous> (request.js:38)
    
    0 回复  |  直到 6 年前