bradtraversy / mern_shopping_list

Shopping List built with MERN and Redux
605 stars 436 forks source link

user validation failed: password: Cast to string failed for value "Promise { <pending> }" at path "password" #49

Open illusionrs opened 4 years ago

illusionrs commented 4 years ago

I am getting this error user validation failed: password: Cast to string failed for value "Promise { }" at path "password"

CLCodeLab commented 3 years ago

This looks like a Mongoose error, when the password is not valid (as string in this case) according to the model's Schema. This most likely occurs with the password 'hasher' (eg. bcrypt) not completing the hashing process before the attempt to create a user... Solution: use async/await with your hashing process, for 'bcrypt' example:

const salt = await bcrypt.genSalt(10)
const hashedPassword = await bcrypt.hash(password, salt)
newUser.password = hashedPassword

Don't forget to put 'async' before the function that's calling it... I hope this helps