2017-06-20 6 views
1

Ich bin sehr neu zu Nodejs/Mongo (mit Mungo). Ich verwende das bcrypt-Modul, um das Passwort aus einem HTML-Formular zu hashen. In meiner Funktion db.create kann ich die Variable storehash nicht in mongodb speichern.Nodejs, bcrypt, Mungo

Ich bekomme keine Fehler, aber es ist nur leer in der Datenbank. Ich habe jede zweite Zeile des Codes überprüft und es scheint zu funktionieren. Ich verstehe nicht, warum ich nicht in der Lage bin, eine Variable als "Passwort: Storehash" zu speichern, während ich etwas wie "Passwort: 'Test'" speichern kann.

Ich bin mir sicher, dass ich etwas mache noob Fehler irgendwo. Ich würde mich über jede Hilfe freuen!

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

+0

Codebeispiele! – num8er

+0

Verwenden Sie den Code-Formatierer, der von SO zur Verfügung gestellt wird, nicht Bilder des Codes –

+0

haha ​​ich denke, ich habe gerade das getan. Vielen Dank! –

Antwort

1

Ihre db.create sollte direkt unter console.log(storehash);, nicht nach dem bcrypt.salt gehen.

Wenn Sie es nach bcrypt.salt setzen, was Sie tun, ist: während Sie Salz für Ihr Passwort sind zu erzeugen und dann die gesalzenen Passwort-Hashing, bist du auch Sachen in Ihrer Datenbank zu speichern db.create verwenden. Sie werden gleichzeitig, nicht sequentiell ausgeführt. Aus diesem Grund erstellen Sie während des Hashens Ihres Kennworts auch einen Benutzer mit db.createohne ein Kennwort.

Mit anderen Worten, es sein sollte:

bcrypt.genSalt(10, function(err,salt){ 
    if (err){ 
     return console.log('error in hashing the password'); 
    } 
    bcrypt.hash(pass, salt, function(err,hash){ 
     if (err){ 
      return console.log('error in hashing #2'); 
     } else { 

      console.log('hash of the password is ' + hash); 
      console.log(pass); 
      storehash = hash; 
      console.log(storehash); 
      db.create({ 

       email: req.body.email, 
       username: req.body.username, 
       password: storehash, 


      }, function(err, User){ 
       if (err){ 
        console.log('error in creating user with authentication'); 
       } else { 
        console.log('user created with authentication'); 
        console.log(User); 
       } 
      }); //db.create 
     } 
    }); 

}); 
+0

Oh ja! Recht. Ich habs. Ich glaube, ich habe sync-Programmierung mit Knoten async Natur vermischt. Es funktioniert jetzt. Danke vielmals !! –

Verwandte Themen