2016-03-21 6 views
0

私は、postgresqlデータベースからデータを取得するためにknexjs ORMを使用しています。複数のデータベース要求からデータをよりきれいに格納できる方法はありますか?現在、私はこれを次のようにしています:複数のコールバックからデータを格納するnode.js javascript

knex('user').where({ 
    approved: true 
}).then(function(data) { 
    context.approved = data; 
    knex('user').where({ 
     unapproved: true 
    }).then(function(data) { 
     context.unapproved = data; 
     console.log(context); //this is my main objective 
    }).catch(function(error) { 
     console.log('error: ' + error); 
    }); 
}).catch(function(error) { 
    console.log('error: ' + error); 
}); 

これ以上のリクエストをする必要がある場合、これは本当に乱雑になることがあります。理想的には、私は次のようなことをしたいと思っています:

function a(callback) { 

    knex('user').where({ 
     approved: true 
    }).then(function(data) { 
     //store this data somehow 
    }).catch(function(error) { 
     console.log('error: ' + error); 
    }); 
}; 

function b(callback) { 

    knex('user').where({ 
     unapproved: true 
    }).then(function(data) { 
     //storethis data somehow 
    }).catch(function(error) { 
     console.log('error: ' + error); 
    }); 
}; 

function c(a, b) { 
    //do an operation with the data returned from functions a and b 
} 

しかし、私はこれを実装する方法を100%確信していません。助けてもらえますか?

ありがとうございます!

答えて

関連する問題