Q promise .then()undefined

时间:2015-02-27 03:56:07

标签: javascript node.js q

我试图异步从MySQL数据库中提取数据,然后使用Q promise(首次尝试)处理结果。我正在使用here列出的Q defer()函数。我创建了一个"包装器"对象我最终将附加到req.db或通过com.db的循环依赖访问(当前如何使用)以允许在我的应用程序中使用api。

节点抛出指向.then回调的错误:

this.go().then(function(stuff){
                          ^
TypeError: Cannot call method 'then' of undefined

我尝试了以下内容:

var com = require('./mainRebuild');

function Base() {
    this.results = [];
    this.query = 'query here';
    this.go = function() {

        com.pool.getConnection(function(err, con) {

            if (err) throw err;
            /*
            THIS WORKS FINE (with callbacks)
             con.query('SELECT * FROM Users', function(err, rows){
                if (err) throw err;
                if (rows){

                  for (var i = 0; i < rows.length; i++){
                    this.results.push(rows[i]);
                    //console.log(rows[i]);
                  }
                }
                con.release();
                return this.results;
              }.bind(this));

              */
            var defer = com.Q.defer();
            con.query('SELECT * FROM Users', defer.makeNodeResolver());
            return defer.promise();
        }.bind(this));
    }
}

function DB() {
    this.getAllUsers = function() {
        this.query = 'SELECT * FROM Users';
        this.go().then(function(stuff) {
            console.log(stuff);
        }, function(err) {
            console.log(err);
        });
    }

}

DB.prototype = new Base();

module.exports = DB;

com对象存在于主应用程序中,它包含所有常见的需求模块。

我也试过(似乎更错了):

this.deffered = com.Q.defer();
com.pool.getConnection(function(err, con) {

            if (err) this.deferred.reject(new Error(err));

            con.query('SELECT * FROM Users', function(err, rows) {
                if (err) throw err;
                if (rows) {
                    this.deferred.resolve(rows);
                }
                con.release();
                return this.deferred.promise.done;
            }.bind(this));

            com.Q.nfcall(this.go()).then...

我找到了几篇关于将Q调整到节点的文章,如this

1 个答案:

答案 0 :(得分:3)

这样的事情应该这样做(&#34;应该&#34;,因为我无法用MySQL测试它)

var Q = require('Q');

function DB(pool) {
    this.asyncQuery = function(sql) {
        return function () { 
            var result = Q.defer(),
                paramsArray = [].slice.call(arguments);
            pool.getConnection(function(err, con) {
                if (err) return result.thenReject(err);
                con.query(sql, paramsArray, result.makeNodeResolver());
                result.promise.finally(function () {
                    con.release();
                });
            });
            return result.promise;
        };
    };
    this.getAllUsers = this.asyncQuery('SELECT * FROM Users');
    this.getUserByName = this.asyncQuery('SELECT * FROM Users WHERE name = ?');
}

module.exports = DB;

用法:

var DB = require('./DB');
var com = require('./mainRebuild');
var db = new DB(com.pool);

db.getAllUsers().then(function (users) {
    console.log(users);
});

db.getUserByName('JohnDoe').then(function (user) {
    console.log(user);
});

var customQuery = db.asyncQuery('SELECT * FROM foo WHERE id = ?');
customQuery(42).then(function (result) {
    console.log(result);
});