【问题标题】:How Can I update each password in my database with hashed bcrypt version?如何使用散列 bcrypt 版本更新数据库中的每个密码?
【发布时间】:2020-06-28 17:23:34
【问题描述】:

如您所见。我正在尝试获取列中的先前密码并使用散列版本对其进行更新。由于某种原因,文档上的保存不会立即触发。所以我尝试使用异步等待,甚至创建自定义异步等待 foreach 来等待回调和保存。然而,Mongoose 似乎在等待所有的保存都进入后才应用保存。

这是我得到的错误。

UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。 (拒绝编号:505) (节点:2440)

const User = require("./models/User");
const bcrypt = require("bcryptjs");
const db = require("./config/keys").mongoURI;


async function hashIt(user) {
    console.log(user);
    bcrypt.genSalt(10, (err, salt) => {
        if (err) console.log(err);
        bcrypt.hash(user.Password, salt, async (err, hash) => {
            if (err) throw err;
            user.Password = hash;
            const id = await user.save();
            console.log(id);
        })
    });
}

async function asyncForEach(array, callback) {
    for (let index = 0; index < array.length; index++) {
        await callback(array[index], index, array);
    }
}

async function mySeed() {
    try {
        User.find({}, async (err, users) => {
            if (err) console.log(err);
            asyncForEach(users, async (user) => {
                await hashIt(user);
            })
        });
    } catch (e) {
        console.log(e);
    }
}

async function fullThing(){
    mongoose.connect(db, { useNewUrlParser: true })
    .then(async () => {
        await mySeed();
        console.log("finished successfully");
    })
}

fullThing();```     

【问题讨论】:

  • 我已经测试了我的解决方案,它有效!

标签: javascript asynchronous mongoose async-await


【解决方案1】:

感谢您的回复。解决方案原来是 w=majority 出于某种原因需要从数据库连接中删除。删除之后。一切都开始正常工作。用 catch 包裹连接确实有助于找到错误。

【讨论】:

    【解决方案2】:

    我在当前的后端系统上运行了一个类似的例程,以下是我如何调整您的代码以适应我的;我的工作正常,因此我希望你的工作也一样。我相信问题在于你没有发现潜在的错误,一开始我经常发生这种情况,有时当我累了时仍然会发生。

    bcrypt.genSalt(10, (err, salt) => {
      if (err) console.log(err);
      bcrypt.hash(user.Password, salt, (err, hash) => {
        if (err) throw err;
        user.Password = hash;
        const id = user
          .save()
          .then(() => {
            console.log("okay");
          })
          .catch(err => console.log(err));
    
      });
    });
    

    如果它不起作用,请告诉我出现的错误。

    附录

    我在下面测试了我的解决方案:

    const bcrypt = require("bcryptjs");
    require("./connection");
    
    //creating user schema
    var mongoose = require("mongoose");
    var Schema = mongoose.Schema;
    
    UserSchema = new Schema({ name: String, password: String });
    var User = mongoose.model("User", UserSchema);
    
    const user = new User({ name: "Jorge Pires", password: "Corona Virus" });
    
    console.log(` Before hashing ${user.password}`);
    
    //---------------------------------------------------------------
    //Uncomment here to save the user before hashing, it will change anyway, therefore no need!
    // user.save();
    // User.create({ name: "Jorge Pires", password: "Corona Virus" });
    //--------------------------------------------------------------
    bcrypt.genSalt(10, (err, salt) => {
      //generates the salta
      if (err) console.log(err);
      bcrypt.hash(user.password, salt, (err, hash) => {
        //creates the hash
        if (err) throw err;
        user.password = hash; //saves the hash
        const id = user
          .save()
          .then(() => {
            console.log(` after hashing ${user.password}`);
            console.log(` Here goes the user id  ${user.id}`);
          })
          .catch(err => console.log(err));
      });
    });
    

    输出样本:

    Before hashing Corona Virus
    we are connected mongoose-tutorial
     after hashing $2a$10$84MqPsiiMGA/KTHKFbytVOD5/su6rXiE7baA2TmsLzPMe.Y45aL9i
     Here goes the user id  5e710a0bd4385c05b0cd827f
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-09
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 2019-07-17
      • 1970-01-01
      相关资源
      最近更新 更多