0
Nunjucksを使用してmongoDBサーバーからHTMLファイルにデータを渡して、読み込み時にレンダリングします。は、nunjucksでhtmlにオブジェクトを渡すことができません
App.js私のデータベース接続ファイルは、この持って
app.get('/', function(req, res) {
database.retrieveComments(req, function(err,result) {
console.log(result);
res.render('index.html', result);
});
});
:
connection.js
retrieveComments: function(req, callback) {
db.collection('comments').find().toArray(function(err,result) {
if (result.length > 0) {
return callback(null, result);
};
});
},
最後に、私のHTMLファイル内に、私はこの部分を持っています:
index.htmlを
<div id="p" class="task" commentId = "1">
1st comment {{ result }}
</div>
私はそれをCONSOLE.LOGときに生じる見ることができますが、私はのLocalServerを参照するとき、HTMLファイルでそれをレンダリングしていないようです。 文字列を渡すだけで、結果オブジェクトを渡すときは表示できません。
ここでは非同期ノードブラックマジックが動作しているのか、キーのNunjucks要素がないのか不思議です。
使用 'res.render( 'index.htmlを'、{結果:結果});' 'または1コメント{{result.smth - プロプ} } ' –
これらのどちらも動作しません。 –