2016-11-19 67 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

那麼它工作?那是什麼打印在控制檯上? –