ile bcrypt-nodejs promisifying NodeJS kullanıyorum, bcrypt-nodejs (https://github.com/shaneGirish/bcrypt-nodejs) ve sözler için Bluebird kullanıyorum. Bu kod ile geldi ve aynı şeyi yapmak için daha iyi bir yol olup olmadığını merak ediyorum.Bluebird
app.post('/api/v1/users/set-password', function(req, res, next) {
users.setPassword(req.body).then(function(result) {
// Store hash in your password DB.
console.log(result[1]);
res.json({
success: true
})
})
.catch(function(err) {
console.log(err);
});
});
Her zaman ile biter: Ben aşağıda users.setPassword
diyoruz başka modülünden sonra
var Promise = require("bluebird"),
bcrypt = Promise.promisifyAll(require('bcrypt-nodejs'));
// ....[some othe code here]
Users.prototype.setPassword = function(user) {
return bcrypt.genSaltAsync(10).then(function(result) {
return bcrypt.hashAsync(user.password, result);
});
};
: Birlikte modülü var "[Hata:. Hiçbir geri çağırma işlevi verildi]" bcrypt.hashAsync
olarak mesaj görünüyor 4 parametre gerektirir. Orijinal, promisified değil hash
yöntemi sadece 3 gerektirir. Ben hashAsync
boş geri arama eklediğinizde, iyi çalışıyor:
Users.prototype.setPassword = function(user) {
return bcrypt.genSaltAsync(10).then(function(result) {
return bcrypt.hashAsync(user.password, result,function() {});
});
};
Bunu yapmak için daha iyi bir yolu var mı yukarıdaki gibi boş geri arama sağlamadan?
DÜZENLEME: Bergi yorumuyla .. fonksiyon soru gönderdiniz zaman şifre sonunda, sadece o kadar alamadım koyacaktır cevaben
. Şimdi bu kadar ilerleme, bir şey olsa çok doğru değilse bana bildirin lütfen:
Users.prototype.setPassword = function(user) {
return bcrypt.genSaltAsync(10).then(function(result) {
return bcrypt.hashAsync(user.password, result, null);
})
.then(function(result) {
// store in database
console.log("stored in database!");
return result;
});
};
Farklı bir yöntem adı kesinlikle kullanacağım. 'users.setPassword' bir şifre koymuş gibi görünmüyor, ama sadece bir tane için bir hash hesaplıyor. Her zamanki gibi – Bergi