amazon-archives / amazon-cognito-js

Amazon Cognito Sync Manager for JavaScript
http://aws.amazon.com/cognito
Apache License 2.0
202 stars 83 forks source link

Error: CognitoSyncManager is not a constructor #57

Open lorcat9 opened 7 years ago

lorcat9 commented 7 years ago

I'm

working on an Ionic App with Angular 4 and TypeScript 2.3.4


import { CognitoUserPool, } from 'amazon-cognito-identity-js';
import * as AmazonCognitoSync from 'amazon-cognito-js';
import * as AWS from 'aws-sdk';

        let client = new AmazonCognitoSync.CognitoSyncManager();
        console.log(client);

Error: ERROR TypeError: __WEBPACK_IMPORTED_MODULE_3_amazon_cognito_js__.CognitoSyncManager is not a constructor.

odinseyeanalytics commented 6 years ago

I have encountered the same problem. Also Angular 4 (4.4.4) and the following packages: "amazon-cognito-identity-js": "^1.16.0", "amazon-cognito-js": "^1.1.0", "aws-sdk": "^2.49.0"

The demo I started with (aws-cognito-apigw-angular) works fine, but I can't add sync to it. I think the issue is more that the entire "amazon-cognito-js" package cannot be found than a typings issue.

I tried the above import as well as the "require" recommendations on a similar issue: https://github.com/aws/amazon-cognito-js/issues/40

I basically get an error in which AWS.CognitoSyncManager() is not returning a defined value: ERROR Error: Cannot set property 'syncclient' of undefined

with the following code:

// import 'amazon-cognito-js';
// import * as AWS from 'aws-sdk';
const AWS = require('aws-sdk');
require('amazon-cognito-js');

//later in code after initializing Identity Pool
     this.syncclient = new AWS.CognitoSyncManager();
     console.log(this.syncclient);

I have not found any combination of import/require that will return an actual value for AWS.CognitoSyncManager().

fgoeckel commented 6 years ago

I have the same problem here. I'm initialising the CognitoSyncManager the following way:

import * as AWS from 'aws-sdk'
import 'amazon-cognito-js';

// After succesfull login
syncClient = new AWS.CognitoSyncManager();

and get the following error: Property 'CognitoSyncManager' does not exist on type 'typeof "./node_modules/aws-sdk/index"'.

If I run the build for a local web deployment with live reload, it interestingly works after a second compilation. So I only get this error at the first compilation attempt. But I'm not able to build it for android, as it directly fails.

Could you make any progress on the issue?

dnalbach commented 6 years ago

I notice you are both importing the SDK with all as AWS instead of getting the AWS export. It is working for me with these imports:

import AWS from 'aws-sdk';
import 'amazon-cognito-js';

// after Google login 

AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: mypoolidstring,
    Logins: { 'accounts.google.com': googleUser.getAuthResponse().id_token }
});

AWS.config.credentials.get(() => {
  const syncClient = new AWS.CognitoSyncManager();
  syncClient.openOrCreateDataset('myDataset', (err, dataset) => {
    dataset.get('myKey', (err, value) => {
      console.log('myKey: ' + value);
    });
 });
});
fgoeckel commented 6 years ago

Thanks for the input, but sadly it doesn't change anything for me. For the moment I'm using a workaround, where I add the following line to 'aws-sdk/index.d.ts':

export var CognitoSyncManager:any;

This way it can find the SyncManage during compilation (although it's empty) and during runtime it is working fine anyways.

dnalbach commented 6 years ago

I see what you mean. Yes, the aws-cognito-js library is appending a class to the aws-sdk namespace at runtime, so would not be found during compilation. This can be seen in the source:


if (AWS === undefined) {
    throw new Error("AWS SDK must be loaded before loading the Sync Manager.");
} else {

    /**
     * Constructs a new Cognito Sync Manager class.
     * @constructor
     */

    AWS.CognitoSyncManager = function (options) {

There are no exports in the CognitoSyncManager, and there are direct SDK property accessors. Frankly, this level of coupling between independent packages seems very strange to me. The aws-cognito-js package is designed as if it were part of the SDK file.

tobinbc commented 6 years ago

I use this workaround:

import * as AWS from 'aws-sdk' import 'amazon-cognito-js'; ... private AWSSync:any private syncManager:any ... //do all other AWS.stuff() ... this.AWSSync = AWS this.syncManager = new this.AWSSync.CognitoSyncManager(); //Now we can do the Typescript import.