我想用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