应该在哪里定义Meteor.methods()? [英] Where should Meteor.methods() be defined?

查看:63
本文介绍了应该在哪里定义Meteor.methods()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://docs.meteor.com/#meteor_methods

我已经在服务器文件夹中的publish.js中尝试过它.

I have tried it in publish.js in my server folder.

我已成功调用Meteor.apply,并尝试从客户端进行服务器调用.我总是得到不确定的答复.

I am successfully calling Meteor.apply and attempting the server call from the client. I always get an undefined response.

推荐答案

在服务器上调用Meteor.methods是正确的.这将定义在特权环境中运行并将结果返回给客户端的远程方法.要返回正常结果,只需从您的方法函数中使用一些JSON值调用return即可.要发出错误信号,请抛出Meteor.Error.

Calling Meteor.methods on the server is correct. That will define remote methods that run in the privileged environment and return results to the client. To return a normal result, just call return from your method function with some JSON value. To signal an error, throw a Meteor.Error.

在客户端上,Meteor.apply始终返回undefined,因为方法调用是异步的.如果您想要方法的返回值,则apply的最后一个参数应该是回调,将以典型的异步回调样式传递给两个参数:errorresult.

On the client, Meteor.apply always returns undefined, because the method call is asynchronous. If you want the return value of the method, the last argument to apply should be a callback, which will be passed two arguments: error and result, in the typical async callback style.

您的服务器代码实际上被调用了吗?您可以通过更新方法中的数据库并查看客户端的缓存是否获取新数据,或从方法主体内部调用console.log并查看终端中流星"过程的输出来进行检查.

Is your server code actually getting called? You can check that by updating the DB in the method and seeing if the client's cache gets the new data, or calling console.log from inside the method body and looking at the output of the "meteor" process in your terminal.

这篇关于应该在哪里定义Meteor.methods()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆