亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么使用簡單的哈希函數會出現非法參數錯誤?

為什么使用簡單的哈希函數會出現非法參數錯誤?

HUWWW 2023-08-24 10:02:22
這是我的代碼const bcrypt = require('bcryptjs');const salt = bcrypt.genSalt(11);const user = {    first: "Donald",    last: "Trump",    password : bcrypt.hash(this.password, salt),    greetUser(password) {      console.log(`Hi, ${this.first} ${this.last} ${this.password}`);    },  };    let password = 'secondhand01';  user.greetUser(password);我跑node --trace-warnings index.jsHi, Donald Trump [object Promise](node:15222) UnhandledPromiseRejectionWarning: Error: Illegal arguments: undefined, object我期望的是散列密碼。為什么終端指向非法參數?
查看完整描述

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...

});


查看完整回答
反對 回復 2023-08-24
  • 1 回答
  • 0 關注
  • 221 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號