Milan
Milan

Reputation: 183

RangeError: Invalid array length on crypto js when i tried to encrypt my password on node.Js Application

I have been developing a node backend API for an ecommerce website for learning purpose. So in that when I tried to encrypt my password for authentication purpose. I am getting an error stating that

...\ECommerceApi\node_modules\crypto-js\core.js:335
words.length = Math.ceil(sigBytes / 4);
                                 ^

RangeError: Invalid array length
    at WordArray.init.clamp (D:\STUDY\React.JS\reactproject\ECommerceApi\node_modules\crypto-js\core.js:335:27)

This is what I am getting when running it through POSTMAN.Here is the code to my Auth Module


const router = require("express").Router();
const User = require("../models/User")
const CryptoJS = require("crypto-js")

//REGISTER
router.post("/register",async (req,res)=>{
    
    const newUser = new User({
        username:req.body.username,
        email:req.body.email,
        password:CryptoJS.AES.encrypt(req.body.password,process.env.PASS_SEC).toString()
 

    });
    try{

        const savedUser = await newUser.save();
        res.status(201).json(savedUser)
        console.log(savedUser)
    }catch(err){
        res.status(500).json(err)
        console.log(err);
    }
});
    

module.exports = router;

Upvotes: 0

Views: 973

Answers (2)

xcrx news
xcrx news

Reputation: 91

On your POSTMAN , make sure your password is string eg password: "123456", and on your MONGOOSE SCHEMA define password as STRING

Upvotes: 0

Milan
Milan

Reputation: 183

The value which I have given in POSTMAN for password in this ecommerce API Project were not of the type string. Which caused this error. I Changed It To String And Hence It Worked.

Upvotes: 1

Related Questions