Javascript 调用Meteor方法会导致内部服务器错误[500]
我从main.js(服务器)调用我的方法,然后在main.js(客户端)上使用Meteor.call(),然后收到一个内部错误[500] 这是我的文件结构 这是控制台错误 main.js(客户端): main.js(server):Javascript 调用Meteor方法会导致内部服务器错误[500],javascript,meteor,Javascript,Meteor,我从main.js(服务器)调用我的方法,然后在main.js(客户端)上使用Meteor.call(),然后收到一个内部错误[500] 这是我的文件结构 这是控制台错误 main.js(客户端): main.js(server): 您只需要在服务器端定义方法 Meteor.startup(() => { // code to run on server at startup if(Meteor.isServer){ Meteor.methods({
您只需要在服务器端定义方法
Meteor.startup(() => {
// code to run on server at startup
if(Meteor.isServer){
Meteor.methods({
addTask: function(name){
if(!Meteor.userId()){
throw new Meteor.Error('Access Denied');
}
Tasks.insert({
name: name,
createdAt: new Date(),
userId: Meteor.userId()
});
},
deleteTask: function(taskId){
Tasks.remove(taskId);
}
});
}
});
好的,我已经找到了答案,我在做
import'../imports/api/task.js'将包含其中的所有变量,答案是我需要从“../imports/api/task.js”导入任务
有人能解释一下差异吗?我在server main.js上定义了它们。检查文件结构我的server和client main.js位于不同的文件夹和文件中,然后您正在正确导入任务集合服务器控制台中的错误是什么?@tomsp Tasks未定义。我添加了控制台错误screenshot@tomsp谢谢兄弟,我集中精力查看console.log错误。我没有注意到服务器控制台的错误,但是如果没有定义任务,那么集合的导入就是问题所在。@tomsp ya。我想导入“../imports/api/task.js”;将实现该文件中的所有变量,我需要像下面这样指定它:从“../imports/api/task.js”导入任务;
import { Meteor } from 'meteor/meteor';
import '../imports/api/task.js';
Meteor.startup(() => {
// code to run on server at startup
Meteor.methods({
addTask: function(name){
if(!Meteor.userId()){
throw new Meteor.Error('Access Denied');
}
Tasks.insert({
name: name,
createdAt: new Date(),
userId: Meteor.userId()
});
},
deleteTask: function(taskId){
Tasks.remove(taskId);
}
});
});
Meteor.startup(() => {
// code to run on server at startup
if(Meteor.isServer){
Meteor.methods({
addTask: function(name){
if(!Meteor.userId()){
throw new Meteor.Error('Access Denied');
}
Tasks.insert({
name: name,
createdAt: new Date(),
userId: Meteor.userId()
});
},
deleteTask: function(taskId){
Tasks.remove(taskId);
}
});
}
});