简体   繁体   English

类型错误:无法通过注册读取未定义的属性(读取“电子邮件”)

[英]TypeError: Cannot read properties of undefined (reading 'email') with registration

I have problem with my if.我的 if 有问题。 There is my code: `有我的代码:`

app.post('/register', redirectHome, async (req, res, next)=>{
    try{
        const email = req.body.email;
        let password = req.body.password;

        emaildb = await db.getUserByEmail(email);

        const isValidEmail = compareSync(email, emaildb.email);
        
        if(isValidEmail){
            err_msg = 'registration done';
            return res.render('register.ejs', {err_msg: err_msg});
            
        }
        else{
            err_msg = 'User already exists!';
            return res.render('register.ejs', {err_msg: err_msg});
        }

` `

There is probably issue with compare, because when I write existing in db email I see "User already exists."比较可能存在问题,因为当我在 db email 中写入 existing 时,我看到“用户已存在”。 and also when I write not existing in db email I see "User already exists.".而且当我在 db email 中写入不存在时,我看到“用户已经存在。”。 So my if condition doesn't work well.所以我的 if 条件不能正常工作。

Consider a case that you do not have an user with a given email, for which emaildb is null or undefined.考虑这样一种情况,您没有给定 email 的用户,emaildb 为 null 或未定义。 Then when you are calling the function compareSync(email, emaildb.email), here emaildb.email is undefined, for which you are not able to compare email and emaildb.email and throws "cannot read properties of undefined", also returning false.然后,当您调用 function compareSync(email, emaildb.email) 时,这里的 emaildb.email 是未定义的,为此您无法比较 email 和 emaildb.email 并抛出“无法读取未定义的属性”,也返回 false。

 try{
        const email = req.body.email;
        let password = req.body.password;
    
        emaildb = await db.getUserByEmail(email);
        
        if(!emaildb){
           // do your work since no user exist with given email
           // return
        }
        else{
            err_msg = 'User already exists!';
            return res.render('register.ejs', {err_msg: err_msg});
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:无法读取未定义的属性(读取“createdTimestamp”) - TypeError: Cannot read properties of undefined (reading 'createdTimestamp') TypeError:无法读取未定义的属性(读取“forEach”) - TypeError: Cannot read properties of undefined (reading 'forEach') TypeError:无法读取未定义的属性(读取“学生”) - TypeError: Cannot read properties of undefined (reading 'students') TypeError:无法读取未定义的属性(读取“addEventListener”) - TypeError: Cannot read properties of undefined (reading 'addEventListener') TypeError:无法读取未定义的属性(读取'indexOf') - TypeError: Cannot read properties of undefined (reading 'indexOf') TypeError:无法读取未定义的属性(读取“国家”) - TypeError: Cannot read properties of undefined (reading 'country') TypeError:无法读取未定义的属性(读取“然后”) - TypeError: Cannot read properties of undefined (reading 'then') TypeError:无法读取未定义的属性(读取“设置”) - TypeError: Cannot read properties of undefined (reading 'set') 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') TypeError:无法读取未定义的属性(读取“问题”) - TypeError: Cannot read properties of undefined (reading 'ques')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM