amazon-archives/amazon-cognito-js

Error: CognitoSyncManager is not a constructor

lorcat9 opened this issue · 6 comments

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.

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:
#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().

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?

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);
    });
 });
});

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.

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.

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.