1 回答

TA貢獻1843條經驗 獲得超7個贊
在對象字面量中,password : bcrypt.hash(this.password, salt) 調用 bcrypt.hash并將其返回值分配給password屬性。在您顯示的代碼中,this 不是指正在創建的對象,而是指同一件事this指的是創建對象文字的位置(模塊的頂層)。由于它沒有屬性password,因此您將傳遞undefined給該函數。
bcrypt.hash還返回一個承諾,正如您從未處理的承諾拒絕之前獲得的輸出中看到的那樣。
您的user對象正在填充硬編碼值,因此您可能打算執行以下操作:
const bcrypt = require('bcryptjs');
const salt = bcrypt.genSalt(11);
bcrypt.hash("secondhand01", salt) // <=== Encrypt the password
.then(hashedPassword => {
// You have it now, you can build and use the object
const user = {
first: "Donald",
last: "Trump",
password : hashedPassword,
greetUser() { // Note I removed the parameter you weren't using here
console.log(`Hi, ${this.first} ${this.last} ${this.password}`);
},
};
user.greetUser(); // Note I removed the unused argument here
})
.catch(error => {
// Handle/report the error...
});
添加回答
舉報