胡說叔叔
2021-12-23 19:28:51
我正在嘗試向我的密碼中添加鹽,但出現此錯誤:(節點:12652)未處理的承諾拒絕警告:類型錯誤:無法讀取未定義的屬性“值”(節點:12652)UnhandledPromiseRejectionWarning:未處理的承諾拒絕。這個錯誤要么是因為在沒有 catch 塊的情況下拋出了異步函數,要么是因為拒絕了一個沒有用 .catch() 處理過的承諾。(拒絕編號:1)(節點:12652)[DEP0018] 棄用警告:不推薦使用未處理的承諾拒絕。將來,未處理的承諾拒絕將使用非零退出代碼終止 Node.js 進程。我的身份驗證/index.jsconst bcrypt = require('bcrypt');const saltRounds = 10;const myPlaintextPassword = 's0/\/\P4$$w0rD';const someOtherPlaintextPassword = 'not_bacon';const salt = bcrypt.genSaltSync(saltRounds);exports.modules = { salt}我的控制器:const Users = require('../models/users');const bcrypt = require('bcrypt');const { salt } = require('../auth/index');const getUsers = ((req,res) => Users.findAll() .then((result) => { res.json(result) }) .catch((error) => { res.json(error) }) )const addUsers = (async (req,res,next) => { const name = req.body.name; const email = req.body.email; let password = bcrypt.hashSync(req.body.password, salt.value); const data = { name, email, password }; console.log(data); Users.create(data) .then((result) => { res.json(result) }) .catch((error) => { res.json(error) });});module.exports = { getUsers, addUsers, Users}
1 回答

至尊寶的傳說
TA貢獻1789條經驗 獲得超10個贊
您導出了一個salt函數。您正在嘗試訪問它的對象,該對象當然是未定義的,因為該對象沒有屬性 name value。所以,它會給你一個UnhandledPromiseRejectionWarning錯誤。Salt函數已經返回一個值。
還有一件事,它應該module.exports在auth/index.js.
您應該從函數返回一個值。這就是您可以重寫整個過程的方式。
index.js
const bcrypt = require('bcrypt');
const saltRounds = 10;
const myPlaintextPassword = 's0/\/\P4$$w0rD';
const someOtherPlaintextPassword = 'not_bacon';
module.exports.salt= function(){
return bcrypt.genSaltSync(saltRounds); }
您可以在控制器級別等待鹽功能。
let password = await bcrypt.hashSync(req.body.password, salt());
而已?,F在,我認為您的代碼將起作用。
添加回答
舉報
0/150
提交
取消