我很新Nodejs/Mongo(與貓鼬)。我正在使用bcrypt模塊來從HTML表單中散列密碼。在我的db.create函數中,我無法在mongodb中存儲變量storehash。Nodejs,bcrypt,Mongoose
我沒有得到任何錯誤,但它只是在數據庫中空白。我已經檢查了代碼的其他所有行,它似乎正在工作。我不明白爲什麼我無法將變量存儲爲「password:storehash」,而我被允許存儲類似「password:'test'」的變量「
我確信我正在做一些noob錯誤的地方。我會很感激任何幫助!
var db = require('../models/users.js');
var bcrypt = require('bcryptjs');
module.exports.createuser = function(req,res){
\t var pass = req.body.password;
\t var storehash;
\t //passsord hashing
\t bcrypt.genSalt(10, function(err,salt){
\t \t if (err){
\t \t \t return console.log('error in hashing the password');
\t \t }
\t \t bcrypt.hash(pass, salt, function(err,hash){
\t \t \t if (err){
\t \t \t \t return console.log('error in hashing #2');
\t \t \t } else {
\t \t \t \t console.log('hash of the password is ' + hash);
\t \t \t \t console.log(pass);
\t \t \t \t storehash = hash;
\t \t \t \t console.log(storehash);
\t \t \t }
\t \t });
\t });
db.create({
email: req.body.email,
username: req.body.username,
password: storehash,
}, function(err, User){
\t if (err){
\t \t console.log('error in creating user with authentication');
\t } else {
\t \t console.log('user created with authentication');
\t \t console.log(User);
\t }
}); //db.create
};// createuser function
代碼示例! – num8er
使用SO提供的代碼格式化程序不發佈代碼的圖像 –
哈哈我想我只是做到了這一點。謝謝! –