weixin_33736649 2013-12-24 15:15 采纳率: 0%
浏览 99

遍历MongoDB文档?

With a basic AJAX web app running locally, a Form POSTs data through Express/Node.js to MongoDB, and a button onClick responds in a rendering of the Mongo document(s) in a div-box.

Using Swig for templating, onClick of the button is only returning a right-brace to appear in the html div-box.

}

How can this be edited to loop through each MongoDB document?

wrapper.html:

{% for go in allDOCs %}
_wrapperGet('{"getting":"{{ keyName }}"}')
{% endfor %}

..where the ajax posts the wrapper data in index.html:

<!-- For the Returned Fields -->
<div id="theResponse">

</div><!-- /.theResponse -->

console.log(returnValue); is listing the mongoDB documents:

{"keyName":"Here's a Value!"}, {"keyName":"Here's another Value!"}

..from app.js

function getAllDOCs(res) {

    db.collection('dbCollectionName').find({}, {"_id":0}).toArray(function (err, docs) {

        console.log("Got the DOCs: " + docs);

        var returnValue = "";
        for (var i = 0; i < docs.length; i++)
        {
            if (returnValue == "")
            {
                returnValue = returnValue + JSON.stringify(docs[i]);
            }
            else
            {
                returnValue = returnValue + ", " + JSON.stringify(docs[i]);
            }
            console.log(docs[i]);
        }

        console.log(returnValue);

        res.render('wrapper', { allDOCs: returnValue });

    });
}

index.html AJAX json parsing:

function handleFINDbuttonResponse(data)
{
  // parse the json string
  var jsonObject = JSON.parse(data);
  $('#theResponse').append( jsonObject.getting );
}
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试
    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿