0
votes

I'm trying to do a batchGetItem with DynamoDB on react-native and it's throwing the following error:

Error: Missing credentials in config
    at Request.VALIDATE_REGION (aws-sdk-react-native.js:10326)
    at Request.callListeners (aws-sdk-react-native.js:10112)
    at Request.emit (aws-sdk-react-native.js:10084)
    at Request.emit (aws-sdk-react-native.js:14062)
    at Request.transition (aws-sdk-react-native.js:13413)
    at AcceptorStateMachine.runTo (aws-sdk-react-native.js:14199)
    at Request.runTo (aws-sdk-react-native.js:13788)
    at Request.send (aws-sdk-react-native.js:13752)
    at features.constructor.makeUnauthenticatedRequest (aws-sdk-react-native.js:6689)
    at features.constructor.getId (aws-sdk-react-native.js:9118) "ConfigError: Missing credentials in config
    at Request.VALIDATE_REGION (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:89572:43)
    at Request.callListeners (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:89495:17)
    at Request.emit (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:89470:10)
    at Request.emit (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:92539:15)
    at Request.transition (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:92283:11)
    at AcceptorStateMachine.runTo (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:92642:13)
    at Request.runTo (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:92362:16)
    at Request.send (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:92352:11)
    at features.constructor.makeUnauthenticatedRequest (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:87518:31)
    at features.constructor.getId (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:88786:17)"

I've authenticated a user, the user has a IAM role and permission to perform the batchGetItem.

Here's a snippet of the relevant code:

    import { Config, CognitoIdentityCredentials, DynamoDB } from 'aws-sdk/dist/aws-sdk-react-native';
    import { AuthenticationDetails, CognitoUser, CognitoUserPool, CognitoUserAttribute } from 'react-native-aws-cognito-js';

    const appConfig = {
      region: 'us-east-1',
      IdentityPoolId: 'us-east-1:foo-bar',
      UserPoolId: 'us-east-1_foo-bar',
      ClientId: '1f2o3o4b5a6r',
    };

    Config.region = appConfig.region;

    class AWS_testpage extends Component {

      constructor(props) {
        super(props);

        this.state = {
          username: 'someUsername',
          password: 'somePassword',
        };

    login() {
        const { username, password } = this.state;

        const authenticationData = {
          Username: username,
          Password: password,
        };

        const authenticationDetails = new AuthenticationDetails(authenticationData);

        const poolData = {
          UserPoolId: appConfig.UserPoolId,
          ClientId: appConfig.ClientId
        };

        const userPool = new CognitoUserPool(poolData);
        console.log('USERPOOOL', userPool);

        const userData = {
          Username: username,
          Pool: userPool
        };

        const cognitoUser = new CognitoUser(userData);

        cognitoUser.authenticateUser(authenticationDetails, {
          onSuccess: (result) => {
            console.log(`ACCESS TOKEN =====> ${result.getAccessToken().getJwtToken()}`);
            Config.credentials = new CognitoIdentityCredentials({
              IdentityPoolId: appConfig.IdentityPoolId,
              Logins: {
                [`cognito-idp.${appConfig.region}.amazonaws.com/${appConfig.UserPoolId}`]: result.getIdToken().getJwtToken()
              }
            });

            alert('Success');
          },
          onFailure: (error) => {
            alert(error);
          },
          newPasswordRequired: () => {
            console.log('NEW PASSWORD REQUIRED');
          }
        });
      }

    onDynamoPress() {
        const dynamodb = new DynamoDB(Config);
        console.log('DYNAMODB', dynamodb);


        const paramsDB = {
        RequestItems: {
        Readings: {
        Keys: [{
          hwid: {
            S: 'ThisIsFake'
          },
          timestamp: {
            S: 'Now'
          }
        }],
        ProjectionExpression: 'received' // this is what element you want back
      }
    }
  };

  dynamodb.batchGetItem(paramsDB, (err, data) => {
    if (err) console.log(err, err.stack); // an error occurred
    else console.log(data);           // successful response

  });
}

If I make the batchGetItem request before authenticating I get the following error:

Error: Missing credentials in config
    at credError (aws-sdk-react-native.js:7490)
    at Config.getCredentials (aws-sdk-react-native.js:7529)
    at Request.VALIDATE_CREDENTIALS (aws-sdk-react-native.js:10313)
    at Request.callListeners (aws-sdk-react-native.js:10108)
    at Request.emit (aws-sdk-react-native.js:10084)
    at Request.emit (aws-sdk-react-native.js:14062)
    at Request.transition (aws-sdk-react-native.js:13413)
    at AcceptorStateMachine.runTo (aws-sdk-react-native.js:14199)
    at Request.runTo (aws-sdk-react-native.js:13788)
    at Request.send (aws-sdk-react-native.js:13752) "CredentialsError: Missing credentials in config
1

1 Answers

0
votes

I created a new Federated Identities pool and it worked!