I am using Knex to do a simple select query from the database. I have returned a value in then function and added await before the async call. But await doesn't wait and always keep running next line with undefined value.
This is User model file with the console.log for the query result, this always get print last:
import knex from '../db/db.config.js';
const User = {};
User.findByNameAndPassword = (username, password) => {
knex('user')
.where({ name: username })
.where({ password: password })
.then(rows => {
console.log(rows);
return rows;
})
.catch((error) => {
console.log(error);
})
}
export default User;
and this is the controller, the console.log here is always printed first:
import User from '../models/user.js';
export const login = async (req, res) => {
let name = req.body.name;
let password = req.body.password;
let user = await User.findByNameAndPassword(name, password)
console.log('user', user);
}