I'm trying to write a small library with re-usable database calls with nano.
db.view('list', 'people', function(error, data) {
if (error == null) {
res.render('people/index', {
people: data.rows
});
} else {
// error
}
});
That can get quite messy when having multiple requests:
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
...
So, the idea was to create a function:
var getPeople = function() {
// do db calls here and return
}
res.render('people/index', {
people: getPeople()
});
But that doesn't work.
How can I solve this and put everything into an external node-js-module.js file?