Async function in mongoose pre save hook not working - javascript

Calling an async function in a pre save hook is returning me undefined for the password. Am I fundamentally misunderstanding async here? I've used it successfully in other areas of my app an it seems to be working fine...
userSchema.pre('save', function (next) {
let user = this
const saltRounds = 10;
const password = hashPassword(user)
user.password = password;
next();
})
async hashPassword(user) {
let newHash = await bcrypt.hash(password, saltRounds, function(err, hash) {
if (err) {
console.log(err)
}
return hash
});
return newHash
}

I think you'd need to handle the promise returned by hashPassword:
hashPassword(user)
.then(password => {user.password = password
next()}
)
I don't think you can turn userSchema.pre into an async function.

Mongoose hooks allow async functions in them. It worked for me. Note that the "next" callback parameter is not needed in async functions as the function executes synchronously.
Here is what a correct async/await version of the code posted in the question would be. Please note that the corrections are marked in bold:
userSchema.pre('save', async function () {
let user = this;
const saltRounds = 10;
const hashed_password = await hashPassword(user.password, saltRounds);
user.password = hashed_password;
}); // pre save hook ends here
async hashPassword(password, saltRounds) {
try {
let newHash = await bcrypt.hash(password, saltRounds);
} catch(err){
// error handling here
}
return newHash;
}

Just to clean things up a bit:
userSchema.pre('save', function(next) {
if (!this.isModified('password')) {
return next();
}
this.hashPassword(this.password)
.then((password) => {
this.password = password;
next();
});
});
userSchema.methods = {
hashPassword(password) {
return bcrypt.hash(password, 10);
},
}
let user = this can be dropped when using an arrow function in then.
When using bcrypt.hash() with no callback, it returns a promise.
async for hashPassword is redundant when using .then when calling

Related

Downside to abusing await as return statement?

I've stumbled upon an interesting but rather hacked way to write linear code with promises in Javascript, as follows:
const return_by_death = new Promise((resolve) => {});
and
const signup = async (req, res) => {
const user = req.body.username;
const pass = req.body.password;
if(!user || !pass) {
res.sendStatus(400);
return;
}
const hash_data = await generate_hash(pass).catch(async (err) => {
res.sendStatus(500);
await return_by_death;
});
const new_account = new models.account.model({
username: user,
salt: hash_data.salt,
hash: hash_data.hash
});
// ...
};
and from my experimentation, it seems to work in the way that if the promise from generate_hash is rejected, it will go into my error handler without moving on to the new_account line. My questions are as follows:
Does this waste memory, by generating promises or execution threads that just hang indefinitely?
Does expressjs hold on to functions passed into app.get(path, func) where it would be keeping track of the signup promise that might never resolve?
Is there a better way to do this?
EDIT: based on the answer/information from #CertainPerformance I came up with the following solution
class PromiseRunError {
constructor(obj) {
this.obj = obj;
}
}
Promise.prototype.run = function() {
return this.catch((err) => {return new PromiseRunError(err)}).then((data) => {
if(data instanceof PromiseRunError) {
return [undefined, data.obj];
}
return [data, undefined];
});
};
and
const [hash_data, hash_gen_err] = await generate_hash(pass).run();
if(hash_gen_err) {
res.sendStatus(500);
return;
}
Yes, using perpetually unresolved Promises here is a problem.
If signup gets called many times, and many Promises that hang are generated, more and more memory will get used over time; each call of signup results in a new closure with user, pass, req, and res variables allocated, which can't be garbage collected until the function ends. (But it'll never end if there's an error.)
It's extremely confusing from a readability standpoint.
If you want to keep things as flat as possible, without surrounding everything in a try/catch, assuming that generate_hash returns a Promise that, if it resolves, will resolve to something truthy, just don't return anything inside the .catch handler, then check if hash_data is truthy or not before continuing:
const signup = async (req, res) => {
const user = req.body.username;
const pass = req.body.password;
if(!user || !pass) {
res.sendStatus(400);
return;
}
const hash_data = await generate_hash(pass).catch(() => {});
if (!hash_data) {
res.sendStatus(500);
return;
}
const new_account = new models.account.model({
username: user,
salt: hash_data.salt,
hash: hash_data.hash
});
// ...
};
I'd prefer try/catch, though:
const signup = async (req, res) => {
try {
const { user, pass } = req.body;
if (!user || !pass) {
res.sendStatus(400);
return;
}
const hash_data = await generate_hash(pass)
const new_account = new models.account.model({
username: user,
salt: hash_data.salt,
hash: hash_data.hash
});
// ...
} catch (e) {
res.sendStatus(500);
return;
}
};

Promise { <pending> } on bcrypt

I am trying to validate a user's password using bcryptjs. I have this function which returns a Promise, however when I get to bycrypt.hash, all i get is Promise { <pending> } Therefore .then() will not execute on undefined. Please help, I've been stuck on this a while
userSchema.methods.verifyPassword = function (password, err, next) {
const saltSecret = this.saltSecret;
const a = async function(resolve, reject) {
console.log('hi4')
console.log('this.saltSecret2', saltSecret);
console.log(password);
const hashed_pass = await bcrypt.hash(password, saltSecret);
console.log('hash', hashed_pass);
const valid = await bcrypt.compare(password, hashed_pass);
if(valid){
console.log('GOOD');
}
};
a();
};
I like to use async-await syntax to handle promises. It is less confusing. and gives the ability of quickly understanding someone else code.
you can make your function an async one. wait until bcrypt does its job
const password = await bcrypt.hash(password, saltSecret);
However bcrypt library provides a function to compare password and the hash
const valid = await bcrypt.compare(password, hashed_pass);
try this
async function(resolve, reject) {
console.log('hi4')
console.log(this.saltSecret);
console.log(password);
const hashed_pass = await bcrypt.hash(password, saltSecret);
console.log('hash', hashed_pass);
const valid = await bcrypt.compare(password, hashed_pass);
if(valid){
console.log('GOOD');
}
};
This line will always return a Promise.
console.log(bcrypt.hash(password, this.saltSecret));
You could always do something like this.
return new Promise(async (resolve, reject) => {
const hash = await bcrypt.hash(password, this.saltSecret);
if (hash == this.password) {
return resolve(true);
}
return reject();
});
bcrypt.hash uses a callback, not a promise (that's what the .then is doing)
You should use it like so:
bcrypt.hash(password, this.saltSecret, (err, hash) => {
...
});

Async function does not await

I am having a problem where an async function does not appear to be waiting. I am calling one async function from another, with the second returning a value after async operations have completed and then the first should be returning this as it has awaited it. But when logging accessToken in the first function it logs before awaiting the return from the second. Where am I going wrong? Thanks in advance.
export const confirmCode = async (verificationId, code) => {
try {
const credential = await firebase.auth.PhoneAuthProvider.credential(verificationId, code);
const accessToken = await authenticate(credential);
console.log(accessToken); // prints undefined as does not wait for above function call?
return accessToken;
} catch (error) {
console.log(error)
// this.showMessageErrorByCode(e.error.code);
}
}
const authenticate = async (credential) => {
try {
await firebase.auth().signInWithCredential(credential).then(result => {
const user = result.user;
user.getIdToken().then(accessToken => {
return accessToken;
});
})
} catch (error) {
console.log(error);
}
}
You should not mix async/await with the older version .then().
Just use it without then() like so:
export const confirmCode = async (verificationId, code) => {
try {
const credential = await firebase.auth.PhoneAuthProvider.credential(verificationId, code);
const accessToken = await authenticate(credential);
console.log(accessToken); // prints undefined as does not wait for above function call?
return accessToken;
} catch (error) {
console.log(error)
// this.showMessageErrorByCode(e.error.code);
}
}
const authenticate = async (credential) => {
try {
let result = await firebase.auth().signInWithCredential(credential); // <-- use await
const user = result.user;
accessToken = await user.getIdToken(); // <-- use await
return accessToken;
} catch (error) {
console.log(error);
}
}
For more detailed explanation, why your code does not work:
You are returning from within a .then() which is not possible
If you would want to use the old way of writing async functions, you would need to use:
return new Promise((resolve, reject) => { /* Code ... */ }); to wrap your function content
resolve(accessToken) instead of return
.then() and .catch() instead of await and try/catch
and some rejects where you can't resolve anything (so probably in the catch block)
But I would suggest you to use the async/await approach, as it is easier to read.

await is only valid in async function - nodejs

I'm using node and express to create server for my app. This is how my code looks like:
async function _prepareDetails(activityId, dealId) {
var offerInfo;
var details = [];
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected correctly to server");
const db = client.db(dbName);
const offers_collection = db.collection('collection_name');
await offers_collection.aggregate([
{ "$match": { 'id': Id} },
]).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
details = docs;
});
})
return details;
}
app.post('/getDetails',(request,response)=>{
var Id = request.body.Id;
var activityId = request.body.activityId;
_prepareDetails(activityId,Id).then(xx => console.log(xx));
response.send('xx');
})
While calling getDetails API I am getting
await is only valid in async function error (At line await offers_collection.aggregate)
I am also getting red underline while declaring async function. Node version I'm using is 11.x. I'm also using firebase API. What am I doing wrong here?
You are missing the async declaration on one of your functions. Here is the working code:
async function _prepareDetails(activityId, dealId) {
var offerInfo;
var details = [];
client.connect(async function(err) {
assert.equal(null, err);
console.log("Connected correctly to server");
const db = client.db(dbName);
const offers_collection = db.collection('collection_name');
await offers_collection.aggregate([
{ "$match": { 'id': Id} },
]).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
details = docs;
});
})
return details;
}
app.post('/getDetails', async (request,response)=>{
var Id = request.body.Id;
var activityId = request.body.activityId;
let xx = await _prepareDetails(activityId,Id);
response.send('xx');
})
Await can only be used in an async function, because await is by definition asynchronous, and therefore must either use the callback or promise paradigm. By declaring a function as async, you are telling JavaScript to wrap your response in a promise. Your issue was on the following line:
client.connect(function(err) {
This is where I added the async as mentioned before.
client.connect(async function(err) {
You will notice I made your route use async also, because you would've had an issue before. Notice the two lines in your original code:
_prepareDetails(activityId,Id).then(xx => console.log(xx));
response.send('xx');
Your response would send before you even made your database call because you are not wrapping the response.send within the .then. You could move the response.send into the .then, but if you are going to use async/await, I would use it all the way. So your new route would look like:
app.post('/getDetails', async (request,response)=>{
var Id = request.body.Id;
var activityId = request.body.activityId;
let xx = await _prepareDetails(activityId,Id);
response.send('xx');
})

Trying to hash a password using bcrypt inside an async function

Following on from this question.
I feel like I'm almost there, but my incomplete understanding of async is preventing me from solving this. I'm basically trying to just hash a password using bcrypt and have decided to seperate out the hashPassword function so that I can potentially use it in other parts of the app.
hashedPassword keeps returning undefined though...
userSchema.pre('save', async function (next) {
let user = this
const password = user.password;
const hashedPassword = await hashPassword(user);
user.password = hashedPassword
next()
})
async function hashPassword (user) {
const password = user.password
const saltRounds = 10;
const hashedPassword = await bcrypt.hash(password, saltRounds, function(err, hash) {
if (err) {
return err;
}
return hash
});
return hashedPassword
}
await dosent wait for bcrypt.hash because bcrypt.hash does not
return a promise. Use the following method, which wraps bcrypt in a promise in order to use await.
async function hashPassword (user) {
const password = user.password
const saltRounds = 10;
const hashedPassword = await new Promise((resolve, reject) => {
bcrypt.hash(password, saltRounds, function(err, hash) {
if (err) reject(err)
resolve(hash)
});
})
return hashedPassword
}
Update:-
The library has added code to return a promise which will make the use
of async/await possible, which was not available
earlier. the new way of usage would be as follows.
const hashedPassword = await bcrypt.hash(password, saltRounds)
By default, bcrypt.hash(password,10) will return as promise. please check here
Example: Run the code,
var bcrypt= require('bcrypt');
let password = "12345";
var hashPassword = async function(){
console.log(bcrypt.hash(password,10));
var hashPwd = await bcrypt.hash(password,10);
console.log(hashPwd);
}
hashPassword();
Output:
Promise { <pending> }
$2b$10$8Y5Oj329TeEh8weYpJA6EOE39AA/BXVFOEUn1YOFC.sf1chUi4H8i
When you use await inside the async function, it will wait untill it get resolved from the promise.
use The method bcrypt.hashSync(), It is Synchronous out of the box.
const hashedPassword = bcrypt.hashSync(password,saltRounds);
Hashing bcrypt asynchronously should be like this
bcrypt.hash(password, saltRounds, function(err, hash) {
if (err) {
throw err;
}
// Do whatever you like with the hash
});
If you are confused with sync and async. You need to read more about them.
There are a lot of good articles out there.
You need to look here in the documentation.
Async methods that accept a callback, return a Promise when callback
is not specified if Promise support is available.
So, if your function call takes in a callback then you can't use await on it since this function signature doesn't return a Promise. In order to use await you need to remove the callback function. You can also wrap it in a Promise and await on it but that's a bit overkill since the library already provides a mechanism to do so.
Code refactor:
try {
// I removed the callbackFn argument
const hashedPassword = await bcrypt.hash(password, saltRounds)
} catch (e) {
console.log(e)
}
const hashedPassword = (password, salt) => {
return new Promise((resolve, reject) => {
bcrpyt.hash(password, salt, (err, hash) => {
if (err) reject();
resolve(hash);
});
});
};
hashedPassword('password', 10).then((passwordHash) => {
console.log(passwordHash);
});
Had same issue...
solved by assigning the Boolean value from a function:
compareHash = (password, hashedPassword) => {
if (!password || !hashedPassword) {
return Promise.resolve(false);
}
return bcrypt.compare(password, hashedPassword);
};
Here the 2 arguments will not be undefined, which is the cause of issue.
And calling the function:
let hashCompare = this.compareHash(model.password, entity.password);

Categories

Resources