我試圖將 bcrypt 散列函式的結果傳遞到下面的用戶模型中。
我似乎無法思考如何有效地解決這個承諾。
代碼如下:
router.post("/", async (req, res) => {
req.body = sanitize(req.body);
// SHA-256
/* const bitArray = sjcl.hash.sha256.hash(req.body.data[2].value);
const passwordHashed = sjcl.codec.hex.fromBits(bitArray); */
const saltRounds = 10;
const password = req.body.data[2].value;
var passwordHashed;
async function hash() {
return await bcrypt.hash(password, saltRounds, function (err, hash) {});
}
const user = new User({
username: req.body.data[0].value,
email: req.body.data[1].value,
password: hash(),
});
try {
await user.save();
res.status(200).json({ msg: "Success" });
} catch (e) {}
});
這就是我到目前為止所嘗試的,這可能是錯誤的
uj5u.com熱心網友回復:
不要將回呼傳遞給bcrypt.hash然后你就可以await了。
const user = new User({
username: req.body.data[0].value,
email: req.body.data[1].value,
password: await bcrypt.hash(password, saltRounds),
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/381749.html
標籤:javascript 表达 承诺
上一篇:TypeError:(intermediatevalue).parseFromString(...).replace不是函式
下一篇:從JSON檔案創建貓鼬模式的問題
