nanoで再利用可能なデータベース呼び出しを使用して小さなライブラリを作成しようとしています。
db.view('list', 'people', function(error, data) {
if (error == null) {
res.render('people/index', {
people: data.rows
});
} else {
// error
}
});
複数のリクエストがある場合、これは非常に厄介になる可能性があります。
db.view('list', 'people', function(error, people) {
db.view('list', 'items', function(error, items) {
db.view('list', 'questions', function(error, questions) {
db.view('list', 'answers', function(error, answers) {
...
res.render('people/index', {
people: people.rows,
items: items.rows,
questions: questions.rows
...
したがって、アイデアは関数を作成することでした。
var getPeople = function() {
// do db calls here and return
}
res.render('people/index', {
people: getPeople()
});
しかし、それはうまくいきません。
これを解決して、すべてを外部のnode-js-module.jsファイルに入れるにはどうすればよいですか?