[英]Why is this not inserting a document into my MongoDB collection?
我有以下代碼嘗試將文檔(記錄)插入MongoDB集合(表)中:
TimeAndSpace = new Mongo.Collection('timeAndSpace');
if (Meteor.isClient) {
Template.addTimeSpaceForm.events({
'submit form': function() {
event.PreventDefault();
var city = "Fort Bragg";
var state = "California";
var yearin = 1958;
var yearout = 1959;
// var city = event.target.city.value;
// var state = event.target.state.value;
// var yearin = event.target.yearin.value;
// var yearout = event.target.yearout.value;
Meteor.call('insertLocationData', city, state, yearin, yearout);
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
Meteor.methods({
'insertLocationData': function(city, state, yearin, yearout) {
console.log('attempting to insert a record');
TimeAndSpace.insert({
ts_city: city,
ts_state: state,
ts_yearin: yearin,
ts_yearout: yearout
});
}
});
沒用 在Chrome開發工具(F12)控制台中輸入以下內容:
TimeAndSpace.find().fetch()
...返回“ []”-顯然表示該集合沒有文檔。
在點擊“添加居住的地方”提交按鈕后,控制台向我顯示的唯一內容是:
XHR finished loading: GET "http://localhost:3000/sockjs/info?cb=jmghsx3ec6".
如果您想/需要知道,這里是HTML:
<head>
<title>timeandspace</title>
</head>
<body>
<h1>A List of the Places I Have Lived</h1>
{{> addTimeSpaceForm}}
</body>
<template name="addTimeSpaceForm">
<form>
<label for="city">City</label>
<input type="text" name="city" id="city">
<br/>
<label for="state">State</label>
<input type="text" name="state" id="state">
<br/>
<label for="yearin">Year Arrived</label>
<input type="text" name="yearin" id="yearin">
<br/>
<label for="yearout">Year Departed</label>
<input type="text" name="yearout" id="yearout">
<br/>
<input type="submit" name="insertdocument" id="insertdocument" value="Add Place Lived">
</form>
</template>
我缺少什么或缺少火力?
因為您犯了一些小的錯字並錯過了錯誤。 例如,您缺少函數中的事件。 然后,您嘗試使用PreventDefault而不是正確的preventDefault。 同樣,方法應僅放在服務器端。 我還自由地使其與您的表格一起使用。 這是代碼和功能流星墊的鏈接。
TimeAndSpace = new Mongo.Collection('timeAndSpace');
if (Meteor.isClient){
Template.addTimeSpaceForm.events({
'submit form': function(event){
event.preventDefault();
var city = event.target.city.value;
var state = event.target.state.value;
var yearin = event.target.yearin.value;
var yearout = event.target.yearout.value;
Meteor.call('insertLocationData', city, state, yearin, yearout);
console.log(TimeAndSpace.find().fetch());
}
});
}
if (Meteor.isServer){
Meteor.methods({
'insertLocationData': function(city, state, yearin, yearout){
console.log('attempting to insert a record');
TimeAndSpace.insert({
ts_city: city,
ts_state: state,
ts_yearin: yearin,
ts_yearout: yearout
});
console.log(TimeAndSpace.find().fetch());
}
});
}
http://meteorpad.com/pad/qgEmW5hv8N4yCaH6s/為什么% 20is%20this%20not%20inserting%20a%20document%20into%20my%20MongoDB%20collection為什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.