findOne 返回 null,即使我的数据库 NodeJS 中有条目

findOne returning me null even though there is entries in my DB NodeJS

提问人:kashif 提问时间:11/5/2023 更新时间:11/5/2023 访问量:66

问:

这是我的用户模型

    module.exports = (sequelize, DataTypes) => {
    const User = sequelize.define("User", {
        email: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: true
        },
        password: {
            type: DataTypes.STRING,
            allowNull: false
        }
    })
    return User
}

我正在尝试查找给定电子邮件的用户表中是否有任何条目

app.post("/login", (req, res) => {
    let email = req.body.email
    User.findOne({ where: { email: email } }).then((result) => {
        if (result == null) {
            console.log("data is null", result)
            res.send("No data found")
        } else {
            res.send(result)
        }
    }).catch((err) => {
        console.log(err, "Error in find one")
    })
})

这是我终端中的输出

Executing (default): SELECT `id`, `email`, `password`, `createdAt`, `updatedAt` FROM `Users` AS `User` WHERE `User`.`email` = '[email protected]';
data is null null

这是我的数据库中的 User 表。This is my User table in DB

mysql node.js express sequelize.js sequelize-cli

评论

0赞 FanoFN 11/6/2023
如果运行此查询,它是否返回相同的长度?SELECT LENGTH(email), LENGTH('[email protected]') FROM Users;
0赞 Anatoly 11/7/2023
只需复制生成的 SQL,并尝试在某些数据库资源管理器(如 DBeaver)中对数据库执行它

答: 暂无答案