[英]How Can I update each password in my database with hashed bcrypt version?
So as you can see.正如你所看到的。 I am trying to take the previous password in the column and update it with the hashed version.
我正在尝试使用列中的先前密码并使用散列版本更新它。 For some reason the save on the document isn't firing right away.
出于某种原因,文档上的保存不会立即触发。 So I tried using async await and even creating a custom async await foreach to await the callback and the save.
所以我尝试使用 async await 甚至创建一个自定义的 async await foreach 来等待回调和保存。 However, Mongoose seems to be waiting for all of the saves to come in before applying the save.
然而,Mongoose 似乎在应用保存之前等待所有保存进来。
This is the error that I get.这是我得到的错误。
UnhandledPromiseRejectionWarning: Unhandled promise rejection.
UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。 (rejection id: 505) (node:2440)
(拒绝ID: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();```
I appreciate the response.我很欣赏回应。 The solution turned out to be that w=majority for some reason needed to be removed from the db connection.
解决方案原来是 w=majority 由于某种原因需要从数据库连接中删除。 After removing that.
去掉之后。 Everything began working fine.
一切开始正常。 Wrapping the connect with the catch did help to find the error.
用 catch 包装连接确实有助于找到错误。
I have a similar routine running on my current back front end system, and below is how I have adapted your code to mine;我在当前的后端系统上运行了一个类似的例程,下面是我如何调整您的代码以适应我的; mine is working, therefore I hope yours will work as well.
我的正在工作,因此我希望你的也能工作。 I believe the problem is that is you are not catching potential errors, that happened to me a lot in the beginning, and sometimes it still happens when I am tired.
我相信问题在于你没有捕捉到潜在的错误,一开始我经常发生这种情况,有时当我累了时它仍然会发生。
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));
});
});
In case it does not work, please, let me know what comes out as error.如果它不起作用,请告诉我什么是错误。
Appendix附录
I have tested my solution below:我在下面测试了我的解决方案:
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));
});
});
Output sample:输出样本:
Before hashing Corona Virus
we are connected mongoose-tutorial
after hashing $2a$10$84MqPsiiMGA/KTHKFbytVOD5/su6rXiE7baA2TmsLzPMe.Y45aL9i
Here goes the user id 5e710a0bd4385c05b0cd827f
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.