简体   繁体   中英

Meteor error invoking method ['questions.insert]: Method 'questions.insert' not found

I am trying to submit a form (that contains test questions) into a mongo collection called Questions. I have referenced the file that runs server side code and i think it should all be working correctly. Here is my code:

//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="IE 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 }); }, }); 

Any help would be greatly appreciated. :)

It looks like you're using Meteor 1.3+'s ES2015 module support and /imports directory lazy loading. With that in mind, in your add.js file you're importing the questions.js file, which contains your questions.insert Method definition. This means your view can properly find this Method on the client side. Method's, however, need to either be made available on both the client and server side, or just server side. To fix your error, you'll want to make sure your Method is also available on the server side, by referencing the questions.js file at startup. Something like:

/server/main.js

import '/imports/startup/server/register_api';

/imports/startup/server/register_api.js

import '../../api/questions.js';

This will trigger your Meteor.methods call on the server, and register the missing questions.insert Method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM