我有这个代码试图将文档(记录)插入MongoDB集合(表):
do
{
//statement here
}
while(condition)
它不起作用。在Chrome开发工具(F12)控制台中输入以下内容:
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
});
}
});
...返回" []" - 显然表明收藏品没有文件。
控制台在点击" Add Place Lived"提交按钮是:
TimeAndSpace.find().fetch()
如果您需要/需要知道,这里是HTML:
XHR finished loading: GET "http://localhost:3000/sockjs/info?cb=jmghsx3ec6".
我失踪或失火的原因是什么?
答案 0 :(得分:2)
因为你犯了一些小错误并且错过了错误。例如,您在函数中缺少事件。然后你尝试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());
}
});
}