sql—当控制台注销时获取空数组将导致nodejs mysql

3pvhb19x  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(241)

我有这个代码,我想检查现有的电子邮件,但我不能得到所有的数据数组包含匹配行的sql。

var email = req.body.email;
        var fullname = req.body.fullname;
        var userPassword = req.body.userPassword
        const saltRounds = 10

        let sql = 'SELECT * FROM registeredusers WHERE id = ?' 
        pool.query(sql, email, (err, results) => {
            console.log(JSON.stringify(results))
            if (err) {
                return res.status(500).json({
                    message: 'Internal server error. Retry... '

                })
            }
            else if (results === 1) {
                return res.status(500).json({
                    message: 'Email already exist.',
                    res: results
                })
            }
1yjd4xko

1yjd4xko1#

只是使用

let sql = 'SELECT COUNT(*) AS cnt FROM registeredusers WHERE email = ?'
        pool.query(sql, [req.body.email], (err, results) => {
            if (err) {
                return res.status(500).json({
                    message: 'Internal server error. Retry... '
                })
            }
            else if (results[0].cnt > 0) {
                return res.status(500).json({
                    message: 'Email already exist.',
                })
            }

相关问题