2015-09-18 59 views
0

我試圖保存被放入這種形式與流星的信息時表不被插入到集合:流星 - 提交

<form class="form-group" id="lost_form"> 
     <label for="item_name">Type</label> 
     <input id="item_name" class="form-control" type="text" placeholder="What is the item? Ex: Water bottle" required/> 

     <label for="item_brand">Brand</label> 
     <input id="item_brand" class="form-control" type="text" placeholder="What brand is the item? Ex: Nalgene" required/> 

     <label for="item_desc">Description</label> 
     <input id="item_desc" class="form-control" type="text" placeholder="Describe the item. Ex: Green, name on bottom" required/> 

     <label for="item_loc">Location</label> 
     <input id="item_loc" class="form-control" type="text" placeholder="Where did you have it last? Ex: Main common room"/> 

     <label for="item_date">Date Missing</label> 
     <input id="item_date" class="form-control" type="date"/> 

     <br> 
     <input id="submit_lost_form" class="btn btn-primary btn-block" type="submit" value="Submit" /> 

    </form> 

的JS我使用把它變成一個集合低於:

LostItems = new Meteor.Collection('lostitems'); 

Meteor.methods({ 
    'insertItem': function(iname, ibrand, idesc, iloc, idate){ 

    LostItems.insert({ 
     user: Meteor.user(), 
     name: iname, 
     brand: ibrand, 
     description: idesc, 
     location: iloc, 
     date: idate 
    }) 
    } 
}); 

if (Meteor.isClient) { 
    Template.lost_form.events({ 
    'submit form': function (event) { 
     event.preventDefault(); 
     var itemName = event.target.item_name.value; 
     var itemBrand = event.target.item_brand.value; 
     var itemDesc = event.target.item_desc.value; 
     var itemLoc = event.target.item_loc.value; 
     var itemDate = event.target.item_date.value; 
     Meteor.call('insertItem', itemName, itemBrand, itemDesc, itemLoc, itemDate); 
    } 
    }); 
} 

但是,只要我提交表單,什麼都不會發生。在開發者控制檯或流星控制檯中沒有錯誤,當我做LostItems.find().fetch()時,沒有任何錯誤。

我對流星很陌生,所以這可能是一個非常愚蠢的問題,但我感謝任何幫助!

+0

這個工程 - 就像你的代碼不包括用戶字段。 http://meteorpad.com/pad/JkxBLRq8XLJTpxvnc/Leaderboard 如果用戶字段是問題,我不認爲它會默默失敗,所以我認爲你的問題可能在別處。 – JeremyK

+0

在你的例子中,你的模板的命名方式並不明顯。你的表單有一個'lost_form',你在'Template.lost_form'上設置你的事件。你是否也將模板命名爲'lost_form'?那麼它應該像@JeremyK所說的那樣工作。否則很明顯爲什麼它不能正常工作。 – val

+0

@Valentin我的模板也被命名爲'lost_form'。該表單是否應具有名稱屬性而不是ID?這會有所作爲嗎? – CastleCorp

回答

-1

我通過在我的軟件包列表中添加insecureyogiben:autoform-tagsautopublish來解決了問題。我認爲autopublish是有所作爲的。我相信有更好的方法來做到這一點,這可能有一些安全缺陷,但這不是一個大項目,也不是存儲敏感數據,所以現在就可以使用。

+0

請看下面的Dean Brettle的回答。它允許您刪除自動發佈軟件包,並解釋了爲什麼在恢復爲0之前看到lost_count簡要顯示1。這稱爲延遲壓縮。 https://www.discovermeteor.com/blog/latency-compensation/ – JeremyK

1

在致insert()的電話中,您可能需要使用Meteor.userId()而不是Meteor.user()。如果沒有autopublish包,則在客戶端上由Meteor.user()返回的文檔可能與服務器上的不同(出於安全原因)不同。這意味着客戶端插入到您的迷你MongoDB中,而服務器端插入到真實的MongoDB中可能會相互衝突。我希望在服務器端插入的結果傳播回客戶端後,客戶端插入被忽略。我不確定它爲什麼不被服務器端插入替換。當你在服務器上運行它時,LostItems.find().fetch()會返回什麼(例如meteor shell)?