遍历MongoDB文档?


                    

有了在本地运行的基本AJAX Web应用程序,Form通过Express / Node.js将数据发布到MongoDB,并且按钮onClick在div框中呈现Mongo文档。</ p>

使用 Swig </ em>进行模板制作,点击按钮 只是返回右括号以出现在html div框中。</ strong> </ p>


  

} </ p>
</ blockquote>

如何对其进行编辑以遍历每个MongoDB文档?</ strong> </ p>

wrapper.html:</ p>

  {%for go in allDOCs%}
_wrapperGet('{“ getting”:“ {{keyName}}”}')
{%endfor%}
</ code> </ pre>

.. ajax在其中将包装器数据发布到index.html中:</ p>

  <!-对于返回的字段->

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

console.log(returnValue); 正在列出mongoDB文档:</ p>

  {“ keyName”:“这是一个值!”},{“ keyName”:“这是另一个值!”}
</ code> </ pre>

..来自app.js </ p>

  function getAllDOCs(res){

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

         console.log(“获得DOC:” + docs);

         var returnValue =“”;
         对于(var i = 0; i <docs.length; i ++)
         {
             如果(returnValue ==“”)
             {
                 returnValue = returnValue + JSON.stringify(docs [i]);
             }
             其他
             {
                 returnValue = returnValue +“,” + JSON.stringify(docs [i]);
             }
             console.log(docs [i]);
         }

         console.log(returnValue);

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

     });
}
</ code> </ pre>

index.html AJAX json解析:</ p>

 功能handleFINDbuttonResponse(数据)
{
   //解析json字符串
   var jsonObject = JSON.parse(data);
   $('#theResponse')。append(jsonObject.getting);
}
</ code> </ pre>
     </ div>

展开原文

原文

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 );
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问