aws-amplify api.get federateInfo with options undefined error

时间:2018-02-23 17:16:39

标签: javascript react-native aws-api-gateway aws-amplify

我正在玩aws-amplify和react-native。我正在尝试在我的主屏幕上进行api.get调用但是仍然遇到错误:

获取项目:key为federatedInfo,选项未定义

我正在调用一个aws-api-gateway dynamodb。我一直在寻找文档修复程序,但大多数都指向使用HOC withAuthenticator。我不想这样做,因为我已经定制了signIn / signUP / Confirm屏幕。任何帮助或只是正确方向的一点都会很棒。这是我的Home.screen代码:

import React, { Component } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import { Button } from "react-native-elements";
import Amplify, { API, Auth } from 'aws-amplify';

Amplify.configure({
 AUTH: {
  identityPoolId: 'identityPoolId',  
  region: 'us-east-1', 
  userPoolId: 'userPoolId', 
  userPoolWebClientId: 'userPoolWebClientId'  
 },
 API: {
  endpoints: [
   {
    name: "biotest1-prod",
    endpoint: "endpointUrl"
   },
  ]
 }
});

class Home extends Component {
 constructor(props) {
 super(props);

 this.state = {
  loading: true,
   notes: []
 }
}

async componentDidMount() {
 try { 
  this.session = await Auth.currentSession();
  console.log(this.session);
 } catch(e) { console.log('no session') }

    const name = 'endpointUrl';
    let path = '/notes';
    let options = {
        headers: {
            Authorization: Auth.currentSession().idToken.jwtToken 
        }
    }

    API.get(name, path, options)
    .then(response => console.log(response))
    .catch(err => console.log(err));
}

userSignOut(){
 Auth.signOut()
  .then(data => {
   console.log(data)
   const { navigate } = this.props.navigation;
   this.props.navigation.navigate("SignIn");
  })
  .catch(err => console.log(err));
}

render() {
 return (
  <View style={styles.container}>

   <View style={styles.buttonView}>
    <Button
     buttonStyle={styles.btn}
     title="SIGN OUT"
     onPress={() => this.userSignOut()}
     />
    </View>

    <Text style={styles.text}>
     This is the Home Page.
    </Text>

   </View>
  );
 }
}
export default Home;

1 个答案:

答案 0 :(得分:0)

我正在调用endpointUrl而不是api的名字。 endpointUrl进入你的amplify.configure。要手动配置,我使用了这个:

Amplify.configure({
 Auth: {

  identityPoolId: 'us-east-1:=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx', // REQUIRED - Amazon Cognito Identity Pool ID     
  region: 'us-east-1', // REQUIRED - Amazon Cognito Region
  userPoolId: 'us-east-x_xxxxxxxxx', // OPTIONAL - Amazon Cognito User Pool ID
  userPoolWebClientId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx', // OPTIONAL - Amazon Cognito Web Client ID
},
API: {
 endpoints: [
  {
    name: "api-name",
    endpoint: "your api endpoint",
    service: "your service",
    region: "us-east-1"
  }
 ]
}
});

您可以在aws控制台中找到api网关信息。希望这会有所帮助。