Skip to content
Advertisement

ERROR TypeError: Cannot read property ‘salt’ of null

I am having troubles trying to encrypt and decrypt the values of local and session storage.

Thank you for your time and your help.

enter image description here

JavaScript

I need to encrypt the values of local and session storage, and decrypt when it’s necessary.

I do not know where it’s the failure.

Which one it’s the easiest way to achieve the encryption?

Advertisement

Answer

The error isn’t that informative, but basically, when decrypting a value with crypto-js, it has a step where it converts string inputs to an “encrypted object” that contains e.g. the salt. If you pass a non-string to the decrypt function, crypto-js assumes it’s already such an object. Therefore, if you pass null, it will later on try to access (null).salt and error.

This basically means your getItem is trying to read a value that isn’t in the storage. Add proper null checks. E.g. if you try to access a value that is null, return that immediately without trying to decrypt it.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement