0

I am trying to finish a login functionality with mysql and express. I got a work_id and a user_password, and I want to use the work_id to find whether the user exists in my database. I use promise to do this, I can log the selected user information in the console, but the promise is always pending, and the web storm console didn't terminate. What I want is a boolean value from the promise, whether the user exists or not. Here is my code: query.js.

const pool = require('./connect');
module.exports = {
    query: function (sqlString, params) {
        return new Promise((resolve, reject) => {
            pool.getConnection(function (err, connection) {
                if (err) {
                    reject(err)
                } else {
                    connection.query(sqlString, params, (err, rows) => {
                        if (err) {
                            reject(err)
                        } else {
                            resolve(rows)
                        }
                        connection.release()
                    })
                }
            })
        })
    }
}

sqlCRUD.js, about the sql statement

const user = {
    queryByWorkId: 'select * from user_info where work_id=?',
    queryAll: 'select * from user_info',
    resetPassword: 'update user_info set user_password = ? where work_id = ?',
};

user.js, I execute the test here.

const Model = require('./main')
const crypto = require('crypto')
const _ = require('./query')
const $sqlQuery = require('./sqlCRUD').user

class User{
  // others
  static findOne(form={}) {
  const { work_id, user_password } = form
  return _.query($sqlQuery.queryByWorkId, work_id)
            .then(res => {
                console.log(res)
                if (res.length > 0) {
                    const u = res[0]
                    return u
                }
                return false
            })
            .catch(err => {
                console.log('User.findOne error', err)
                return {
                    errmsg: JSON.stringify(err)
                }
            })
    }

Here is my test, in user.js

const test = () => {
    const form = {
        work_id: '007',
        user_password: 'root',
    }
    const r = User.findOne(form)
    console.log('r', r)
}

And this is the output: I am not allowed to embed a picture here, so SO generates a link

I got confused about this: in my query.js file, I return a promise, in my User.findOne(form={}) method, I call it with a then and catch, return _.query($sqlQuery.queryByWorkId, work_id).then(res => console.log(res)).catch(err => console.log(err)), but the console did't terminate, and I just got a Promise { }.

What's wrong with my code? How can I get a value returned from a then clause in promise when select data using mysql? Thanks in advance.

Shadow
  • 33,525
  • 10
  • 51
  • 64
uncle liu
  • 37
  • 7
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Roamer-1888 Jul 29 '18 at 03:54
  • @uncleliu, your `test` function runs without anything that makes it await a promise resolve. – OfirD Jul 29 '18 at 09:43
  • @HeyJude , I don't understand it. Add a then clause after findOne, like this? const r = User.findOne(form).then(res => console.log(res)) – uncle liu Jul 29 '18 at 12:07
  • @uncleliu, right. Did that solve it? If not, what happens? – OfirD Jul 29 '18 at 12:17
  • @HeyJude , no. It didn't help. The web storm console printed the res out, but after that, the console is still running, I had to close it manully, which wasn't what I want. – uncle liu Jul 29 '18 at 13:37
  • @Roamer-1888 , hi, I don't that post helps. I read several times, but still I can not figure it out. Can you tell me where was wrong? – uncle liu Jul 29 '18 at 13:38
  • @uncleliu, if promises weren't involved, what would cause the web storm console to terminate? – Roamer-1888 Jul 29 '18 at 16:18
  • @Roamer-1888 just press the stop button, or ctrl-c – uncle liu Jul 29 '18 at 23:08

0 Answers0