我正在尝试将一个表单(包含测试问题)提交到名为Questions的mongo集合中。我引用了运行服务器端代码的文件,我认为它应该都能正常工作。 这是我的代码:
//add.html
<template name="add">
<h3>This is the add questions page</h3>
<form class="add-questions">
<label>Subject</label> <br>
<input type="text" name="subject" placeholder="Maths" value="subject"> <br>
<label>Topic</label> <br>
<input type="text" name="topic" placeholder="I.E. Algebra" value="topic"> <br>
<label>Level</label> <br>
<input type="number" name="level" value="3"> <br>
<label>Marks</label> <br>
<input type="number" name="marks" value="5"> <br>
<label>Date</label> <br>
<select name="month">
<option> - Month - </option>
<option value="jan">January</option>
<option value="feb">February</option>
<option value="mar">March</option>
<option value="apr">April</option>
<option value="may">May</option>
<option value="jun">June</option>
<option value="jul">July</option>
<option value="aug">August</option>
<option value="sep">September</option>
<option value="oct">October</option>
<option value="nov">November</option>
<option value="dec">December</option>
</select>
<select name="year">
<option> - Year - </option>
<option value="16">2016</option>
<option value="15">2015</option>
<option value="14">2014</option>
<option value="13">2013</option>
<option value="12">2012</option>
<option value="11">2011</option>
<option value="10">2010</option>
<option value="9">2009</option>
<option value="8">2008</option>
<option value="7">2007</option>
<option value="6">2006</option>
<option value="5">2005</option>
<option value="4">2004</option>
<option value="3">2003</option>
<option value="2">2002</option>
<option value="1">2001</option>
<option value="0">2000</option>
</select> <br>
<label>Question</label> <br/>
<textarea name="question" class="question" id="question" form="add-question" placeholder="Please enter the question here as plane text" value="questionArea"></textarea> <br>
<label>Awnser</label> <br/>
<textarea name="answer" class="answer" form="add-question" placeholder="Please enter the question here as plane text" value="answerArea"></textarea> <br>
<input id="submitbutt" type="submit" name="submit" value="Submit"> <a href="/" id="cancel">Cancel</a> <br>
</form>
</template>
//add.js
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { ReactiveDict } from 'meteor/reactive-dict';
import { Questions } from '../../api/questions.js';
import './add.html';
Template.add.events({
'click #cancel'(event, instance) {
event.preventDefault();
if(confirm("Are you sure you want to cancel?"))
{
window.location.assign("/");
}
},
'submit .add-questions'(event) {
event.preventDefault();
const target = event.target;
const questionId = Random.id;
const questionSubject = target.subject.value;
const questionTopic = target.topic.value;
const questionLevel = target.level.value;
const questionMarks = target.marks.value;
const month = target.month.value;
const year = target.year.value;
const questionDate = month + " " + year;
const questionQuestion = $('textarea.question').get(0).value;
const questionAnswer = $('textarea.answer').get(0).value;
console.log("adding: ", questionId, questionSubject,
questionTopic, questionLevel, questionMarks,
questionDate, questionQuestion, questionAnswer);
Meteor.call('questions.insert', questionId, questionSubject,
questionTopic, questionLevel, questionMarks,
questionDate, questionQuestion, questionAnswer);
console.log("added");
//redirect
},
});
Template.add.helpers({
thisQuestion() {
const questionId=FlowRouter.getParam("questionId");
console.log("Adding question: ", questionId);
return Questions.findOne({"_id": questionId});
},
});
// questions.js
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
export const Questions = new Mongo.Collection('questions');
if (Meteor.isServer) {
// This code only runs on the server
// Only publish events that belong to the current user
Meteor.publish('questions', function questionsPublication() {
return Questions.find();
console.log("published questions");
//return Venues.find();
});
}
Meteor.methods({
'questions.insert'(id, subject, topic, level, marks, date, question, answer) {
console.log("run questions.insert");
// Make sure the user is logged in before inserting a task
if (! this.userId) {
throw new Meteor.Error('not-authorized');
}
Questions.insert({
id,
subject,
topic,
level,
marks,
date,
question,
answer
});
},
});
非常感谢任何帮助。 :)
答案 0 :(得分:3)
您似乎正在使用Meteor 1.3 +的ES2015模块支持和/imports
目录延迟加载。考虑到这一点,在您的add.js
文件中,您要导入questions.js
文件,其中包含您的questions.insert
方法定义。这意味着您的视图可以在客户端正确找到此方法。但是,方法需要在客户端和服务器端提供,或者只在服务器端提供。要修复错误,您需要通过在启动时引用questions.js
文件来确保您的方法在服务器端也可用。类似的东西:
<强> /server/main.js 强>
import '/imports/startup/server/register_api';
<强> /imports/startup/server/register_api.js 强>
import '../../api/questions.js';
这将触发您在服务器上的Meteor.methods
来电,并注册缺少的questions.insert
方法。