[英]Node.js+Handlebars+Express how to reference the handlebars templates?
[英]Handlebars, JS and Node
我正在嘗試獲取服務器從MongoDB發送的數據。 常規GET:
.get('/test', function(req, res){
mongo.collection('collection').find({ _id:req.user._id.toString()
}).toArray( function(err, doc){
res.render('partials/map',
{ docs : doc });
}
});
});
即發送帶有文檔的數組到客戶端。 然后,我想在客戶端上操作數據,所以我在客戶端上的javascript中執行以下操作:
<script>
var docs = '{{docs}}';
console.log(typeof(docs));
console.log(docs);
var obj = new Object(docs);
console.log(obj);
var arr = new Array(docs);
console.log(arr);
console.log(JSON.stringify(docs));
</script>
但是,我無法弄清楚如何進行實際操作,因為上述操作僅在控制台中提供了以下輸出:
string
[object Object],[object Object],[object Object]
String {"[object Object],[object Object],[object Object]"}
["[object Object],[object Object],[object Object]"]
"[object Object],[object Object],[object Object]"
如何處理數據? 我知道這是一個包含三個文檔的數組,但是僅嘗試docs [0]會在[object Object]中給出第一個字符(即“ [”)。 另外,JSON.parse(docs)只會返回錯誤,因為docs已經以某種方式已經是一個對象。
在服務器上對對象進行字符串化,然后在客戶端上對其進行訪問。
//server
res.render('partials/map', {docs: JSON.stringify(doc)})
//client
var docs = {docs}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.