2015-11-10 43 views
1

我需要從PouchDB數據庫獲取所有文檔。任何人都可以告訴我如何從回調函數中獲取'doc'?我想將它作爲響應對象返回,並將其作爲控制檯輸出。pouchdb - 從數據庫獲取所有數據

var db = new pouchdb('meetups'); 
db.allDocs({ 
    include_docs: true, 
    attachments: true 
}).then(function (err,res) { 
    console.log("Result..."+res); 
    res.json({'users':res}); 
}).catch(function (err) { 
    console.log(err); 
}); 

回答

1

我想你想要的是以下幾點:

var db = new pouchdb("meetups"); 
db.allDocs({ 
    include_docs: true, 
    attachments: true 
}).then(function (result) { 
    console.log(result); 
    res.json({"users": result.rows}); 
}).catch(function (err) { 
    console.log(err); 
}); 

如果因爲您的catch回調來處理錯誤,所以你只能在你的then回調有一個參數(result) 。 result變量將包含一些關於結果的元數據(例如total_rows)以及rows屬性,該屬性將成爲數據庫中所有文檔的數組。

+0

謝謝......我通過使用「\t db.get(response.rows [i] .id,function(err,doc){JSON.stringify(doc.title).....」 –

+0

@ RajeshKJeyapaul這個工作,但會導致額外的(和不必要的)對數據庫的查詢。在我的例子中,'result.rows'屬性應該包含你需要的所有數據,例如'result.rows [i] .doc '。如果你想遍歷行:'result.rows.map(function(row){console.log(row.doc);});'' –