草庐IT

javascript - meteor 错误调用方法 ['questions.insert]: Method ' questions.insert' not found

coder 2023-11-03 原文

我正在尝试将一个表单(包含测试问题)提交到一个名为 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});
	},
});

//问题.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
    });
  },
});

如有任何帮助,我们将不胜感激。 :)

最佳答案

看起来您正在使用 Meteor 1.3+ 的 ES2015 模块支持和 /imports 目录延迟加载。考虑到这一点,在您的 add.js 文件中导入 questions.js 文件,其中包含您的 questions.insert 方法定义.这意味着您的 View 可以在客户端正确找到此方法。然而,方法需要要么在客户端和服务器端都可用,要么只在服务器端可用。要修复您的错误,您需要通过在启动时引用 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 方法。

关于javascript - meteor 错误调用方法 ['questions.insert]: Method ' questions.insert' not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39079130/

有关javascript - meteor 错误调用方法 ['questions.insert]: Method ' questions.insert' not found的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  3. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  4. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  5. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  6. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  7. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  8. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  9. ruby-on-rails - 新 Rails 项目 : 'bundle install' can't install rails in gemfile - 2

    我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="

  10. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

随机推荐