V
V
Viktor Yanyshev2019-03-11 16:40:22
JavaScript
Viktor Yanyshev, 2019-03-11 16:40:22

How to hang a promise on sqlite methods and is a promise needed?

I think it's easier to give an example of the code and its current result, and a description of the desired result.

Database
'use strict';

class Database {

    constructor() {
        this._sqlite3 = require('sqlite3').verbose();
        this._conn = new this._sqlite3.Database(__dirname + '/database.sqlite');
    }


    findOne(sql, params) {
        let self = this;
        return self._conn.get(sql, params, function (err, row) {
            if(err) throw err;
            console.log(row);
            return row;
        })
    }

user
'use strict';

class User extends Database {
    constructor() {
        super();
    }

   getWorker() {
       let sql = 'select u.*, w.password from users u, workers w where u.id = w.user_id and u.type_id = ?';
       return super.findOne(sql, [1]);
   }

}

Page initialization
let user = new User();
console.log(user.getWorker()); // Приходит объект Database, хотя ожидаю Object просто

At first comes Database when pulled getWorker and only then Object with the User.
5c86654398fa4369465431.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question