F
F
femalemoustache2015-08-28 16:34:22
PostgreSQL
femalemoustache, 2015-08-28 16:34:22

Node.js (Express.js + Sequelize.js ORM): Why is the query not getting results?

There is an application based on Express. Database - postgresql, ORM - Sequelize. This is a POST request handler in which I need to get the user from the database by username.

// /routes/login.js
router.post('/', function(req, res, next) {
  models.User.findAll({ 
    where: { 
      username:req.body.username 
    } 
  }).then(function(user) {
    console.log(user);
  });
  res.render('login');
});

The command line displays the query text and the result (an empty array):
Executing (default): SELECT "id", "username", "password" FROM "Users" AS "User" WHERE "User"."username" = 'admin';
[]

Although there is such a user, I check using psql with a query
SELECT id, username, password FROM Users WHERE username = 'admin';

Tried the same with .findOne():
router.post('/', function(req, res, next) {
  models.User.findOne({ 
    username: req.body.username 
  }).then(function(user) {
    console.log(user);
  });
  res.render('login');
});

Request log:
Executing (default): SELECT "id", "username", "password" FROM "Users" AS "User" LIMIT 1;
null

Those. .findOne() ignored the { username:req.body.username } argument when generating the request. The field req.body.username comes, checked.
Tell me what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kotofey, 2015-08-28
@kotofey

I don't see any obvious errors. I would double check Sequeliz's connection settings.
As for findOne, here you have the wrong syntax, it should be the same as for findAll:

models.User.findOne({ 
    where: { 
      username : req.body.username 
    } 
  })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question