Javascript 如何使用哈希bcrypt版本更新数据库中的每个密码?

Javascript 如何使用哈希bcrypt版本更新数据库中的每个密码?,javascript,asynchronous,mongoose,async-await,Javascript,Asynchronous,Mongoose,Async Await,如你所见。我正在尝试获取列中以前的密码,并用哈希版本更新它。由于某些原因,文档上的保存没有立即启动。因此,我尝试使用async await,甚至创建一个自定义的async await foreach来等待回调和保存。然而,猫鼬似乎在应用保存之前等待所有的保存 这就是我得到的错误 未处理的PromisejectionWarning:未处理的承诺拒绝。此错误源于在没有catch块的异步函数中抛出,或者拒绝未使用.catch()处理的承诺。(拒绝id:505) (节点:2440) const User

如你所见。我正在尝试获取列中以前的密码,并用哈希版本更新它。由于某些原因,文档上的保存没有立即启动。因此,我尝试使用async await,甚至创建一个自定义的async await foreach来等待回调和保存。然而,猫鼬似乎在应用保存之前等待所有的保存

这就是我得到的错误

未处理的PromisejectionWarning:未处理的承诺拒绝。此错误源于在没有catch块的异步函数中抛出,或者拒绝未使用.catch()处理的承诺。(拒绝id:505) (节点:2440)

const User=require(“./型号/用户”);
const bcrypt=require(“bcryptjs”);
const db=require(“./config/keys”).mongoURI;
异步函数hashIt(用户){
console.log(用户);
bcrypt.genSalt(10,(错误,盐)=>{
if(err)console.log(err);
bcrypt.hash(user.Password、salt、async(err、hash)=>{
如果(错误)抛出错误;
user.Password=hash;
const id=wait user.save();
console.log(id);
})
});
}
异步函数asyncForEach(数组、回调){
for(让index=0;index{
if(err)console.log(err);
asyncForEach(用户,异步(用户)=>{
等待hashIt(用户);
})
});
}捕获(e){
控制台日志(e);
}
}
异步函数fullThing(){
connect(db,{useNewUrlParser:true})
。然后(异步()=>{
等待我的种子();
console.log(“成功完成”);
})
}
fullThing();`

我在当前的后端和前端系统上运行了一个类似的例程,下面是我如何将您的代码改编为我的代码;我的正在工作,因此我希望你的也能工作。我相信问题在于你没有抓住潜在的错误,这在开始的时候经常发生在我身上,有时在我累的时候也会发生

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

我感谢你的答复。结果证明,由于某种原因,需要从db连接中删除w=多数。在移除之后。一切都开始运转良好。用catch包装connect确实有助于找到错误。

我已经测试了我的解决方案,它很有效!
Before hashing Corona Virus
we are connected mongoose-tutorial
 after hashing $2a$10$84MqPsiiMGA/KTHKFbytVOD5/su6rXiE7baA2TmsLzPMe.Y45aL9i
 Here goes the user id  5e710a0bd4385c05b0cd827f