amazon-archives / amazon-cognito-js

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

ReferenceError: window is not defined #45

Open exabugs opened 7 years ago

exabugs commented 7 years ago

On plain JS (not Browser) environment (ex. unit test), CognitoSyncManager don't work properly.

ReferenceError: window is not defined
    at new a (src/_lib/amazon-cognito.min.js:35:23975)
    at new a (src/_lib/amazon-cognito.min.js:35:13090)
    at new AWS.CognitoSyncManager (src/_lib/amazon-cognito.min.js:35:377)
    at server/server_test.js:43:28
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickDomainCallback (internal/process/next_tick.js:122:9)

code is below.

      aws.config.credentials.get(() => {
        const syncClient = new aws.CognitoSyncManager();
        syncClient.openOrCreateDataset('myDataset', (err, dataset) => {
          dataset.put('myKey', 'myValue', (err, record) => {
            dataset.synchronize({
              onSuccess: (data, newRecords) => {
                // Your handler code here
                console.log();
              }
            });
          });
        });
      });
johnborges commented 7 years ago

What JS environment are you using? In nodeJS there is no window object.

exabugs commented 7 years ago

Yes, I use this on Nodejs6.9 + Mocha at UnitTest environment. Production environment is on the modern browser, Babel + Webpack compiled JS.

Test with browser is too difficult, so I'd like to test it on simple environment.

This library is wrapper HTML5 WebStorage and only use on Browser, isn't it ?

itrestian commented 7 years ago

Yes, it uses window local storage for storing data. That wouldn't be available in node.

oieesah commented 7 years ago

So what can be used instead of window? Can we make it work with node?

itrestian commented 7 years ago

You could use a package that substitutes that in node such as node-localstorage. However, we have only tested this library in a browser environment.

https://www.npmjs.com/package/node-localstorage