简体   繁体   English

流星错误调用方法['questions.insert]:找不到方法'questions.insert'

[英]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. 我正在尝试将一个表单(包含测试问题)提交到名为问题的mongo集合中。 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 //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. 看来您正在使用Meteor 1.3+的ES2015模块支持和/imports目录延迟加载。 With that in mind, in your add.js file you're importing the questions.js file, which contains your questions.insert Method definition. 考虑到这一点,在add.js文件中,您将导入questions.js文件,其中包含您的questions.insert方法定义。 This means your view can properly find this Method on the client side. 这意味着您的视图可以在客户端正确找到此Method。 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. 要解决您的错误,您需要通过在启动时引用questions.js文件来确保您的方法在服务器端也可用。 Something like: 就像是:

/server/main.js /server/main.js

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

/imports/startup/server/register_api.js /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. 这将触发服务器上的Meteor.methods调用,并注册缺少的questions.insert方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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