对于盐和哈希密码,我使用了bcrypt方法,但它显示为错误=新错误('数据和盐参数所需');

发布于 2025-01-28 16:06:18 字数 1416 浏览 3 评论 0 原文

对于盐和哈希密码,我使用了bcrypt方法,但它显示了 错误=新错误('需要数据和盐参数'); 我认为我的代码有问题,请我需要帮助; 电子商务网站是我的项目,因此,我在用户面板中创建了注册表

* 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'
}  

这是我的代码,但是错误

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

Error: data and salt arguments required

如何解决问题。

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 ..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十雾 2025-02-04 16:06:18

根据您遇到的错误,它看起来像 bcrypt.hash 失败,因为 userdata.password 可能未设置。
确保使用请求正确获取userdata.password。
如果您使用Express,则可能不会转换为JSON,因此请添加中间件将数据解析为JSON,例如:

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

查看以前的问题&答案:

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:

风追烟花雨 2025-02-04 16:06:18

谢谢,我得到了解决方案。 用户helpers.js 中的问题。

用户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)                    
                    });
                    });
                 
         }
    ```

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)                    
                    });
                    });
                 
         }
    ```
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文