问题描述
前一段时间,我决定从PHP切换到node.在我的第一个项目中,我不想使用任何ORM,因为我认为我不需要花太多时间来学习另一件事(目前我正在学习节点和角度),因此我决定使用mysql软件包而不使用它.还要别的吗.重要的是要说我有一些复杂的查询,并且我不想从sctratch中学习如何使用9000 ORM节点之一来使它们正常工作,这是我到目前为止所做的:
Some time ago I decided to switch from PHP to node. In my first projects I didn't want to use any ORM since I thought that I didn't need to complicate my life so much learning another thing (at the moment I was learning node and angular) therefor I decided to use mysql package without anything else. It is important to say that I have some complex queries and I didn't want to learn from sctratch how to make them work using one of the 9000 ORM node have, This is what I've been doing so far:
thing.service.js
thing.service.js
Thing.list = function (done) {
db.query("SELECT * FROM thing...",function (err,data) {
if (err) {
done(err)
} else {
done(null,data);
}
});
};
module.exports = Thing;
thing.controler.js
thing.controler.js
Thing = require('thing.service.js');
Thing.list(function (err,data) {
if (err) {
res.status(500).send('Error D:');
} else {
res.json(data);
}
});
我如何使用bluebird来实现这种功能?我已经尝试过了,但是....我在这里寻求帮助.这就是我尝试过的
how can I promisify this kind of functions using bluebird ? I've already tried but .... here I am asking for help. This is what I tried
var Thing = Promise.promisifyAll(require('./models/thing.service.js'));
Thing.list().then(function(){})
推荐答案
我已经这样做了,并且运行正常.
I have done this way and it is working fine.
const connection = mysql.createConnection({.....});
global.db = Bluebird.promisifyAll(connection);
db.queryAsync("SELECT * FROM users").then(function(rows){
console.log(rows);});
这篇关于如何使用bluebird来推广MySql函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!