gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:02:50 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com