2016-11-19 90 views
0

使用Meteor框架版本1.2.1 我有一个表单,它将数据添加到Mongo集合。然而,我发现javascript代码没有错误地执行(并且所有调试console.log消息都如我所期望的那样出现),但是该集合仍然没有我刚插入的数据。问题是这种情况是随机发生的。所以有时插入工作,有时插入不起作用。与upsert命令类似的问题。我的html和js代码粘贴在下面(注意我没有删除自动发布和不安全的软件包)。摸不清什么在我的代码的问题和需要帮助在这个问题上MongoDB插入和插入不能正常工作 - 使用流星框架工作

JS代码

ProjectList = new Mongo.Collection("projectList"); 
ListItem = new Mongo.Collection("listItem"); 
if (Meteor.isClient) { 
Template.create.events({ 
    'submit .add_chapter': function (event) { 
    var addtoWhat; 
    var chapName = event.target.chapName.value; 
    if (event.target.addToWhat.value) { 
     if (event.target.addToWhat.value.length > 0) { 
     addtoWhat = event.target.addToWhat.value; 
     } else { 
     } 
    } else { 
    } 
    if (addtoWhat) { 
     var addToWhatItem = ListItem.findOne({name:addtoWhat}); 
     var item = { 
     name : chapName, 
     list : [], 
     } 
     var id = ListItem.insert(item); 
     if (id) { 
     addToWhatItem.list.push(id); 
     if (!ListItem.upsert({_id:addToWhatItem._id}, addToWhatItem)) { 
      alert("Unable to upsert"); 
     } 
     } else { 
     alert("Unable to insert"); 
     } 

    } else { 
     var projList; 
     if (!ProjectList.findOne({projectId:"123"})) { 
     projList = { 
      projectId : "123", 
      list : [], 
     }; 
     var topid = ProjectList.insert(projList); 
     if (topid) { 
      console.log ("Top Insert succesful with id " + topid); 
     } else { 
      console.log ("Error Top Insert unsuccesful with id "); 
     } 
     } 
     projList = ProjectList.findOne({projectId:"123"}); 
     var item = { 
     name : chapName, 
     list : [], 
     } 
     var id = ListItem.insert(item); 
     projList.list.push(id); 
     if(!ProjectList.upsert({_id:projList._id}, projList)) { 
     console.log("Upsert failed"); 
     } else { 
     console.log("Upsert successful"); 
     } 
    } 
    }, 
    'submit .gen_tree' : function(event) { 
    getElements(); 
    } 

}); 

function getElements() { 
} 

if (Meteor.isServer) { 
    Meteor.startup(function() { 
    // code to run on server at startup 
    }); 
} 

HTML代码

  <head> 
      <title>test_hierarchy2</title> 
     </head> 

     <body> 
      <h1>Welcome to Meteor!</h1> 
      {{> create}} 
     </body> 

     <template name="create"> 
      <form class="add_chapter" method="post"> 
      <label>addToWhat</label> 
      <input type="text" name="addToWhat"> 
      <label>chapName</label> 
      <input type="text" name="chapName"> 
      <button type="submit">Add</button> 
      </form> 
      <form class="gen_tree" method="post"> 
       <button type="submit">generate</button> 
      </form> 
     </template> 

回答

0

试试这个

ProjectList.update({_id:projList._id}, projList, {upsert:true}, function(error,doc){ 
    if(error){ 
     console.log(error); 
    } 
    else{ 
     console.log(doc); 
    } 
}) 
+0

这并没有帮帮我。不过,我希望在控制台上打印一些消息,看是否有错误,但没有收到任何消息。 – user2434843

+0

还有一件事。当收集更新正确时,回调会有一些打印。 – user2434843

+0

那么它工作?那是什么打印在控制台上? –