amazon-cognito-js icon indicating copy to clipboard operation
amazon-cognito-js copied to clipboard

ReferenceError: window is not defined

Open exabugs opened this issue 8 years ago • 5 comments

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

exabugs avatar Feb 01 '17 07:02 exabugs

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

johnborges avatar Feb 01 '17 14:02 johnborges

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 ?

exabugs avatar Feb 02 '17 00:02 exabugs

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

itrestian avatar Feb 08 '17 05:02 itrestian

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

oieesah avatar Feb 20 '17 14:02 oieesah

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

itrestian avatar Feb 20 '17 17:02 itrestian