sequelize.js
पहले के हुक में विशेषताओं को संशोधित करें
खोज…
परिचय
रिकॉर्ड बनने से पहले हमें विशेषताओं को संशोधित करने की आवश्यकता हो सकती है। उपयोगकर्ता द्वारा बनाए जाने पर मेरा उपयोग मामला पासवर्ड एन्क्रिप्ट कर रहा था।
हुक डॉक्स यहाँ http://docs.fterelizejs.com/en/v3/docs/hooks/#instance-hooks है । यह एक लायब्रेरी / फ़ंक्शन के साथ इसका उपयोग करने के तरीके को प्रलेखित करता है जो एक Promise लौटाता है। लेकिन कॉलबैक के साथ उपयोग का मामला स्पष्ट रूप से प्रलेखित नहीं है।
वाक्य - विन्यास
- beforeCreate (उदाहरण)
- पहले (उदाहरण, विकल्प, fn)
उदाहरण एक पुस्तकालय के साथ काम करना जो वादा का उपयोग नहीं करता है
function cryptPassword(password, callback) {
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err)
return callback(err);
bcrypt.hash(password, salt, null, function(err, hash) {
return callback(err, hash);
});
});
}
User.beforeCreate((user, options, cb) => {
cryptPassword(user.password, (err, hash) => {
if (err) return cb(err);
user.password = hash;
// invoking the finish callback is important!
return cb(null, options);
});
});
उदाहरण एक पुस्तकालय के साथ काम करना जो वादा का उपयोग नहीं करता है
User.beforeCreate(function(user, options) {
return hashPassword(user.password).then(function (hashedPw) {
user.password = hashedPw;
});
})
Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow