作为蓝鸟诺言的nodejs sqlite3 db.run [英] nodejs sqlite3 db.run as a bluebird promise

查看:291
本文介绍了作为蓝鸟诺言的nodejs sqlite3 db.run的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Express应用中使用sqlite3. 基本上,我得到一个休息请求,根据休息请求,我查询一个外部REST请求.在来自外部请求的响应之间从原始REST请求传入的数据,然后进行更新或插入我的sqlite3表中.

I'm attempting to use sqlite3 in a express app. Basically, I get a rest request, based on the rest request, I query an external REST request. Between the response from the external request & the data passed in from the original REST request, I then do an update or insert into one of my sqlite3 tables.

我遇到的问题是,在db.run(sqlStatement, paramArray, function(err))中,function(err)是一个回调,其中err是错误或nil.除此之外,如果err参数为null,则this引用包含2个属性,其中一个告诉我该语句修改的行数. ( https://github.com/mapbox/node-sqlite3/wiki/API#databaserunsql-param--callback 供参考)

The problem I'm running into, is that in db.run(sqlStatement, paramArray, function(err)), the function(err) is a callback where err is either an error, or a nil. In addition to that, IF the err param is null, then the this reference contains 2 properties, one of which tells me the number of rows modified by the statement. (https://github.com/mapbox/node-sqlite3/wiki/API#databaserunsql-param--callback for reference)

事实是,我在sqlite3模块上运行了bluebird的promisifyAll,然后使用生成的

Thing is, I run bluebird's promisifyAll on the sqlite3 module, and then use the resultant

db.runAsync(sqlStatement, paramArray).then(err) {
    console.log(this) //results in a null
}

因此,我无法真正确定是否进行了任何更新.

So I'm unable to actually figure out whether anything was updated.

我的整个代码部分看起来都像这样:

My entire section of code looks a little like this:

function handleRequest(req, res) {

    //this returns the response as the first object
    request.getAsync('http://www.example.com', reqObj)         
        .then(prepareDbObjects) //prepares an array of objects to update the DB with
        .then(attemptUpdate)
        .then(function(rowsUpdated) {
            res.json(rowsUpdated)
        }
}

function attemptUpdate(updateObjs) {
    var promiseMap = Promise.map(updateObjs, function(singleObj) {
        return updateOrInsertObj(singleObj)
    }
    return promiseMap
}


function updateOrInsertObj(singleObj) {
    return db.runAsync(sqlStatement, singleObj)
        .then(function(err) {
            if(err) {
                //handle error
            } else {
                console.log("this should be an object with 2 properties", this)
                //but instead it is null
            }
        }
}

推荐答案

我去看了node-sqlite3代码,我很确定它在成功的回调函数中返回this的方式,而不是作为实际的参数,就是问题所在.这意味着,由于没有适当的返回值,即使尝试使用bluebird的multiArgs=true参数也无法正常工作.

I went looking in the node-sqlite3 code, and I'm pretty sure the way it returns this in the successful callback function, rather than as an actual param, is the problem. Which means even attempting to use bluebird's multiArgs=true parameter didn't work since there wasn't a proper return value.

因此,我尝试将db.run函数包装在我自己的自定义promise方法中,这似乎可以解决问题.

So I attempted to wrap the db.run function in my own custom promise method, and that seemed to do the trick.

我确实做到了:

function runCustomAsync(sql, params) {
    return new Promise(function(resolve, reject) {
        db.run(sql, params, function cb(err) {
            if(err) {
                var responseObj = {
                    'error': err
                }
                reject(responseObj);
            } else {
                var responseObj = {
                    'statement': this
                }
                resolve(responseObj);
            }
        });
    });
}

db.runCustomAsync = runCustomAsync;
module.exports = db;

它与通常的处理方式略有不同.我现在返回的对象可能包含this对象,也可能包含err对象.

It IS a little different from how it's normally handled. I'm now returning an object that may contain either the this object, or it might contain the err object.

在我最初的请求中,我现在要做

In my original request, I now do

db.runCustomAsync(sqlStatement, params)
    .then(function(dbResponseObj) {
        if(dbResponseObj.error) {
            //handle error
        } else {
            //do stuff with dbResponseObj.statement
        }
    })

这篇关于作为蓝鸟诺言的nodejs sqlite3 db.run的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆