我試圖將一個表單(包含測試問題)提交到稱爲問題的mongo集合中。我引用了運行服務器端代碼的文件,我認爲它應該都能正常工作。 這裏是我的代碼:流星錯誤調用方法['questions.insert]:方法'questions.insert'找不到
//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?"))
{
\t window.location.assign("/");
}
},
'submit .add-questions'(event) {
\t event.preventDefault();
\t
\t const target = event.target;
\t const questionId = Random.id;
\t const questionSubject = target.subject.value;
\t const questionTopic = target.topic.value;
\t const questionLevel = target.level.value;
\t const questionMarks = target.marks.value;
\t const month = target.month.value;
\t const year = target.year.value;
\t const questionDate = month + " " + year;
\t const questionQuestion = $('textarea.question').get(0).value;
\t const questionAnswer = $('textarea.answer').get(0).value;
\t console.log("adding: ", questionId, questionSubject,
\t \t questionTopic, questionLevel, questionMarks,
\t \t questionDate, questionQuestion, questionAnswer);
Meteor.call('questions.insert', questionId, questionSubject,
questionTopic, questionLevel, questionMarks,
questionDate, questionQuestion, questionAnswer);
console.log("added");
//redirect
},
});
Template.add.helpers({
\t thisQuestion() {
\t \t const questionId=FlowRouter.getParam("questionId");
\t console.log("Adding question: ", questionId);
\t \t return Questions.findOne({"_id": questionId});
\t },
});
//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({
\t \t id,
\t \t subject,
\t \t topic,
\t \t level,
\t \t marks,
\t \t date,
\t \t question,
\t \t answer
});
},
});
任何幫助將不勝感激。 :)
在您的應用程序目錄結構中,您是否找到了「questions.js」文件?它在'/ imports'目錄下嗎? – hwillson
是的,它在進口/ API –