sequelize findOne doesn't find a userId when that usedIf is in the db

Viewed 489

I am using node.js/express/sequelize lo log in a user. I'm testing making the requests in postman to see if it gets anything. I am testing a user and pw that I know are in the db but my findOne query in sequelize returns null even though the tested name is in the db. Here's the query in question:

    let {  userId, pw } = req.body
  try{
    const foundUser = await User.findOne({ where: { userId: userId } });
    if (foundUser === null) {
        return res.status(401).send({ auth: false, token: null,error: 'name not found' });
    } else {
        const passwordIsValid = bcrypt.compareSync(pw, foundUser.pw);
        if (!passwordIsValid) return res.status(401).send({ auth: false, token: null,error: 'name not found' });
        const token = jwt.sign({ id: foundUser.id}, config.secret, {
            expiresIn: 86400 // expires in 24 hours
          });
        res.status(200).json({auth:true,token:token});
    }

  } catch (err) {
    res.status(500).json(err);
  }

}

my json

I have body-parser implemented for jsonenter image description here on index js and the routing works as the userId and pwd values make it to the findOne query. However it still says that there are no results after the query. Terminal runs the following (userId is 'slim')

Executing (default): SELECT id, username, userId, pw, createdAt, updatedAt FROM users AS users WHERE users.userId = 'slim';

'slim' is both in the Json and in the 'userId' section of the db

1 Answers

it turns out the issue was because there was more than one 'slim' in the userId section of my db, it was acting funny. When I entered a truly unique value for userId, it returned the token as expected. Maybe it seems obvious but it's important that that value called for authentication in unique.

Related