我调用 Meteor 方法但它显示错误消息:
Method 'users.insertotp' not found [404]
但不知何故 console.log 被执行了,为什么?
我做错了什么?
你可以在这里看到代码github .
文件:imports/api/db.js
export const Otp = new Mongo.Collection('otp');
Meteor.methods({
'users.insertotp' (otp) {
// if(!this.userId) {
// throw new Meteor.Error('not-authorized');
// }
console.log('otp',otp);
return Otp.insert({
otp,
userId: this.userId,
updatedAt: moment().valueOf()
});
}
});
文件:imports/ui/Signup.js
Meteor.call('users.insertotp',1234);
最佳答案
您的 imports/api/db.js
文件已导入到客户端,但未导入到服务器。
你的 client/main.js
定义了一些路由,其中 imports/ui/Signup.js
导入了 imports/api/db.js
并立即调用方法。
另一方面,您的 server/main.js
仅导入 imports/api/users.js
,而不会导入其他 api 文件。
因此您的客户端知道该方法,因此它能够在浏览器控制台中打印日志,这要感谢 Meteor 延迟补偿 a.k.a. optimistic ui a.k.a. 客户端 stub 。但是您的服务器不知道它,因此它以 404 错误响应。
只需在服务器主入口文件中导入 db 文件,Meteor 就会发挥它的魔力。
关于javascript - Meteor 方法 404 错误但登录控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56640722/