amazon-archives / aws-sdk-react-native

AWS SDK for React Native (developer preview)
Apache License 2.0
631 stars 68 forks source link

undefined is not an object (evaluating 'cognitoClient.initWithOptions') #16

Closed whoknowsjas closed 7 years ago

whoknowsjas commented 7 years ago

Trying to test this out but running into an issue. At face value it appears that the NativeModules.AWSRNCognitoCredentials is not loading... but not sure if symptom or cause. Ideas?

Note I am running android simulation, and here is the abbrev. code: ... import {AWSLambda} from 'aws-sdk-react-native-lambda'; import {AWSCognitoCredentials} from 'aws-sdk-react-native-core';

var functionName = "sampleFunction"; var cognitoRegion = "us-east-1"; var identity_pool_id = "us-east-1:xxxx"; var serviceRegion = "us-east-1";

... async Setup() : Promise { Promise.all([AWSCognitoCredentials.initWithOptions({"region":cognitoRegion,"identity_pool_id":identity_pool_id})]).then(()=>{ AWSLambda.initWithOptions({"region":serviceRegion}); }); return; }

async _invokeRequest() { await this.Setup(); var shouldResolve = false; var req = { "FunctionName" : function, "InvocationType" : "RequestResponse", "LogType" : "None", "ClientContext" : this.encode(JSON.stringify({"System":"android"})), "Payload" : JSON.stringify({"email":"testing1234"}) }; try{ console.log("testing..."); var response = AWSLambda.Invoke(req); if(response.LogResult){ console.error("response.LogResult should not be present"); shouldResolve = false; return shouldResolve; } if(!response.Payload){ console.error("response.Payload is not present"); shouldResolve = false; return shouldResolve; } if(response.Payload.Length === 0){ console.error("response.Payload.Length is 0. Response payload length: " + response.Payload.Length); shouldResolve = false; return shouldResolve; } if(response.StatusCode === 0){ console.error("response.StatusCode is 0. Response code: " + response.StatusCode); shouldResolve = false; return shouldResolve; }

 } catch(e) {
  console.error(e);
  shouldResolve = false;
  return shouldResolve;
 }

} ...

ksegla commented 7 years ago

I have the same problem. Android. Samsung S6. Are there things missing from the README. Do we need to add external libraries (the Android AWS SDK) to make this work?

ksegla commented 7 years ago

This fixed the issue (a bad installation more likely) for Android. Add this line compile 'com.amazonaws:aws-android-sdk-core:2.2.+' in the dependencies of android/app/build.gradle if it's not there.

[How I got there: I went to my node_modules/aws-sdk-react-native-core/android/build.gradle saw this: dependencies { compile 'com.facebook.react:react-native:+' compile 'com.amazonaws:aws-android-sdk-core:+' } From there, I got to this http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/setup.html

used option 1 (my IDE is not Android Studio but I do use it) and voilà! :)]

whoknowsjas commented 7 years ago

Thanks for the tip, although it didn't work for me. I added the entry in the build.gradle and confirmed it downloaded the dependency in the build... but still get: "undefined is not an object (evaluating 'cognitoClient.initWithOptions')" when running.

I'm using Deco for IDE but I even tried running it in the terminal and still no good. Any other ideas?

ksegla commented 7 years ago

I'd suggest may be looking into your own node_modules/aws-sdk-react-native-core/ (may be the lambda one too) and see if there are any other dependencies out there. Right now, I moved on trying the other services (meaning I am not using the lambda service yet. if you are, you'd probably have to add its dependencies too I guess) Sorry no other ideas. Good luck!

ksegla commented 7 years ago

(you may want to go with the jars too [Option 2 in the link]. Though I tried it earlier and it generated an error about how the class AmazonClientException was coming from different sources)

ksegla commented 7 years ago

Back to my code, I realised I did a number of things when I first got the error. Here is what I recall doing manually. (I hope it helps but general advice is to track down the dependencies.)

___android/settings.gradle include ':aws-sdk-react-native-core' project(':aws-sdk-react-native-core').projectDir = new File(settingsDir, '../node_modules/aws-sdk-react-native-core/android')

___in my MainApplication.java import com.amazonaws.reactnative.core.AWSRNCorePackage;

@Override
protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
      ...
      , new AWSRNCorePackage()
  );
}
whoknowsjas commented 7 years ago

That last part worked! Importing and loading explicitly in the MainApp class works... like you said must be something wrong with the install. For now this will do. Thanks again for the help!

bomalley commented 7 years ago

I'm having this same issue under iOS. I suspect it's something to do with the code in the node-modules folders not compiling against the SDK framework files I've included in the Xcode project for my app.

Can anyone suggest how to get past this roadblock?