Node.js EventLoop操作未完成?

时间:2014-02-26 16:57:43

标签: javascript jquery html node.js mongodb

这是关于Node / Express / MongoDB应用程序,带有AJAX调用

  

出于某种原因,selector.html()事件正在替换.html的html   选择器,但现在不是。

唯一的变化是在后端引入了editAll()函数,该函数在.get上传递MongoDB数据,在通过ajax将其传递到前端之前进行一些计算。 /强>

而不是浏览器控制台与GET http://localhost:9999/

一起编写btn.click
[13:47:57.240] GET http://localhost:9999/ [HTTP/1.1 200 OK 5ms]
[13:47:57.174] "Getting All Actions"
[13:47:57.192] "value of action with index 0 = 4"
[13:47:57.193] "value of action with index 1 = 8"
[13:47:57.193] "value of action with index 2 = 9"
[13:48:03.704] GET http://localhost:9999/ [HTTP/1.1 200 OK 7ms]
[13:48:03.640] "Getting All Actions"
[13:48:03.661] "value of action with index 0 = 4"
[13:48:03.661] "value of action with index 1 = 8"
[13:48:03.661] "value of action with index 2 = 9"
  

..控制台会为前端出现的一组重复 div写一整套“回复”

[13:29:24.244] GET http://localhost:9999/ [HTTP/1.1 200 OK 5ms]
[13:29:24.182] "Getting All Actions"
[13:29:24.197] "value of doc with index 0 = 24"
[13:29:24.197] "value of doc with index 1 = 28"
[13:29:24.197] "value of doc with index 2 = 29"
...
[13:29:24.198] "value of doc with index 6 = 24"
[13:29:24.198] "value of doc with index 7 = 28"
[13:29:24.199] "value of doc with index 8 = 29"

据我所知,后端工作正常,因为唯一的3 docs in the db被传递给eventAll()函数,该函数再次传递相同的3 docs

function getAll(res) {

    db.collection('demo').find().sort( { value: 1 } ).toArray(function (err, docs) {
        console.log("Got the docs: " + utils.inspect(docs));

        //res.json({docs: docs}); THIS WORKS PERFECTLY,
                                  but the eventAll() pass causes this frontend issue

            /* pass and rebuild the data array before we 'json' it */
        var editedDocs = editAll(docs); 
        res.json({docs: editedDocs});

    });
}

调用editAll()函数传递数据时,eventloop操作是否未结束?

function editAll(allDocs) {
  var returnedValue = [];
  for (var i=0, len=allDocs.length; i < len; ++i){
      //does some calculations
    var newVal = {_id:allDocs[i]._id,title:allDocs[i].title,value:allDocs[i].value};
    returnedValue.push(newVal);
  }
  console.log(returnedValue);
  return (returnedValue);
}

为什么每个btn.click都是另一组div添加到#result

这是通过gotAllSuccess在btn.click上构建一些html的ajax .get调用

$('#getBtn').click(function() {
  console.log('Getting All');
  $.get('http://localhost:9999').
   done(gotAllSuccess).
   fail(gotAllFailure);
});

function gotAllSuccess(result){

  var docs = result.docs;
  var html = '';
  var doc;
  for (var i=0, len=docs.length; i < len; i++){
    doc = docs[i];
        console.log("value of doc with index " + i + " = " + doc.value);
    html += "<div class='rResult' id='rResult" + i + "'>"+doc.title+"</div><br>";
  }
  $('#result').html(html);
}

1 个答案:

答案 0 :(得分:0)

基本上,您的代码是正常的,必须按预期工作。 我建议您检查逻辑并在将其写入响应之前记录editedDocs值。

似乎您以某种方式查询结果缓冲

您可以按如下方式重写editAll函数(以消除循环和所有其他变量,以及所有可能的副作用):

function editAll(allDocs) {
    return allDocs.map(function(it){
        return { 
            _id:it._id,
            title:it.title,
            value:it.value 
        }
    })
}