Hydrostatic
Hydrostatic

Reputation: 45

Cannot read property 'encrypt' of undefined

When I use crypto-js to encrypt text, it throws an error Cannot read property 'encrypt' of undefined. What's wrong with my code?

...
const Cookies = require('js-cookie');
const request = require('browser-request');
const CryptoJS = require('crypto');

class OssHelper extends Helper {
    addFetchEvent (urlFunction) {
        const createTime = new Date().getTime();
        const encryptUuid = CryptoJS.AES.encrypt(Cookies.get('CLIPUUID'), createTime);
    };
}
...

Upvotes: 1

Views: 10254

Answers (2)

Christoph Bimminger
Christoph Bimminger

Reputation: 1018

The error tells you that "AES" is undefined. Please check how you should use CryptoJS. It seems you did not instanciate an AES object. Shouldn't you require(crypto/aes) and assign it to an AES constant?

const AES = require('crypto-js/aes');
...
const encryptUuid = AES.encrypt

See https://www.npmjs.com/package/crypto-js

Upvotes: 1

ilia
ilia

Reputation: 339

you are installing crypto-js with command (if you are using npm of course) npm install crypto-js, not crypto, (crypto is other built in module of nodeJS), so you got to require crypto-js not crypto with code: var CryptoJS = require('crypto-js')

Upvotes: 0

Related Questions