我有一个基于express(Node.js)的应用程序,其中有一个名为User
的模型。这是模型
var User = mongoose.model(
"users",
new Schema({
name: String,
email: String,
password: String,
token: String,
createdAt: String,
updatedAt: String,
tempToken : String,
verificationCode : String,
isActive: { type: Boolean, default: true }
})
);
我正在向数据库中添加用户,并在添加用户时使用Sendgrid
电子邮件发送API向用户的电子邮件发送验证。这是我的主要功能,分别与相应的api相关。
export async function addUser(req, res) {
try {
let body = parseBody(req)
console.log(body)
body.createdAt = Date.now()
body.updatedAt = Date.now()
let resp = await saveUser(body)
if (resp) {
generateResponse(resp.success, resp.message, resp.data, res)
}
else {
generateResponse(false, "Can't add user", null, res)
}
}
catch (err) {
generateResponse(false, 'Error occured, 404 not found!', err, res)
}
}
这是我的辅助函数saveUser
export async function saveUser(obj) {
let chk = await User.find({ 'email': obj.email })
if (chk.length == 0) {
let hashVal = cryptr.encrypt(obj.password)
var verification_code = await generateRandomNumber(5)
if (hashVal) {
obj.password = hashVal
obj['verificationCode'] = verification_code
console.log('in hashvalue if',obj)
let emailProps = {
to : obj.email,
from : "fahadsubzwari924@gmail.com",
subject : `Email Verification`,
html : `<div style="width : 100%; height : 100px; background-color:
lightgray;text-align : center"><p>Your Verification code is :
${verificationCode}<p></div>`
}
sgMail.send(emailProps,async(err,result)=>{
console.log('in send mail')
if (err) {
console.log('err : ',err)
return ({ success: false, message: "Error in email sending", data: err
})
}else{
console.log('in else i.e user save')
var userObj = new User(obj)
let user = await userObj.save()
return ({ success: true, message: "User added", data: user })
}
})
}
else {
return ({ success: false, message: "Error in hashing password" })
}
}
else {
return ({ success: false, message: "Already Exist", data: null })
}
}
我将控制台日志放在许多地方以调试该问题。代码调用一直进行到此控制台console.log('in hashvalue if',obj)
为止,但此后不再出现。我不知道是什么问题。我该如何解决?