我想用nano来编写一个带有可重用数据库调用的小型库。CouchDB和nano.js的回调和返回
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()
});
但是,这并不工作。
我该如何解决这个问题,并将所有内容放入外部节点-js-module.js文件中?
答案中的视频链接已关闭,在此处找到副本:http://vimeo.com/19519289 – 2013-12-07 10:40:21