0

for salt and hashing passwords i used Bcrypt method but its showing , error = new Error('data and salt arguments required'); i think my code have the problem please i need help please; eCommerce website is my project so ,i created signup form in user panel

*user.js

const userHelpers=require('../helpers/user-helpers')
 
router.get('/signup',(req,res)=>{
   res.render('user/signup')
})

router.post('/signup',(req,res)=>{
  userHelpers.doSignup(req.body).then((response)=>{
     console.log(response);
  })

user-helpers.js

var db=require('../config/connection')
 var collection=require('../config/collections')
 const bcrypt=require('bcrypt')

module.exports={
    doSignup:(userData)=>{
        return new Promise(async(resolve,reject)=>{
            // var salt =  await bcrypt.genSalt(10);
            userData.Password= await bcrypt.hash(userData.Password ,10)
                db.get().collection(collection.USER_COLLECTION).insertOne(userData).then((data)=>{

                    resolve(data) 
                });
                     
            });
          
    }

collection.js

module.exports={
    PRODUCT_COLLECTION:'product',
    USER_COLLECTION:'user'
}  

this is my code but the error shoing

error = new Error('data and salt arguments required');
                ^

Error: data and salt arguments required

how to fix the problem ..

Harshad.N
  • 58
  • 2
  • 11

2 Answers2

0

According to the error you get, it looks like the function bcrypt.hash fails because userData.Password might not be set. Make sure that you get the userData.Password correctly with the request. If you use express you might not have a conversion to JSON so add a middleware to parse the data to JSON, like:

const express = require('express');
const app = express();

app.use(express.json());

app.post('/', function(request, response){
  console.log(request.body);      // your JSON
   response.send(request.body);    // echo the result back
});

app.listen(3000);

Take a look at these previous questions & answers:

Ron Hillel
  • 160
  • 6
0

Thank you, I got the solution. The problem in the user-helpers.js.

user-helpers.js


    const bcrypt = require('bcrypt')
         
    module.exports={
        doSignup:(userData)=>{
            return new Promise (async(resolve,reject)=>{
          
            userData.password=await bcrypt.hash(userData.password,10)
                   
                      db.get().collection(collection.USER_COLLECTION).insertOne(userData).then((data)=>{
                        resolve(data) 
                        // console.log(userData.password)
                        // console.log(userData.email)                    
                    });
                    });
                 
         }
    ```
Harshad.N
  • 58
  • 2
  • 11