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

查看:25
本文介绍了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 并查看meteor"进程的输出来检查这一点在您的终端中.

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天全站免登陆