Meteor.methods 返回未定义 [英] Meteor.methods returns undefined

查看:14
本文介绍了Meteor.methods 返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是meteor 0.6.4.

I'm using meteor 0.6.4.

Meteor.methods({
  random: function(top){
    var random = Math.floor((Math.random()*(top+1)));
    return random;
  }
});

每当我执行时它返回 undefined

It returns undefined whenever I execute

Meteor.call('random', 10);

有什么办法可以解决这个问题吗?

Any ideas how I can get past this?

推荐答案

这是一个完全正常的行为:Meteor 中的服务器方法调用是 记录是异步的:

This is a perfectly normal behavior: server method calls in Meteor are documented to be asynchronous :

在客户端,如果不传递回调并且不在存根内部,则调用将返回 undefined,并且您将无法获取该方法的返回值.

On the client, if you do not pass a callback and you are not inside a stub, call will return undefined, and you will have no way to get the return value of the method.

这意味着当您请求 Meteor.call 方法在服务器上远程执行时,本地方法调用是非阻塞的,并立即返回 undefined.当该方法在服务器上被调用时,它会将结果异步发送到客户端,因此您应该使用回调模式检索它:

It means that when you ask for a Meteor.call method to execute remotely on the server, the local method call is non blocking and returns undefined immediately. When the method has been called on the server it will send the result asynchronously to the client, so you should retrieve it using the callback pattern :

Meteor.call("myMethod", arguments..., function(error, result){
  if(error){
    console.log(error.reason);
    return;
  }
  // do something with result
});

一旦将服务器方法结果发送回客户端,就会在客户端调用匿名回调函数.

The anonymous callback function will be called on the client as soon as the server method result is sent back to the client.

Meteor 中有另一个微妙的特性使我刚才所说的无效:延迟补偿和方法存根.如果服务器方法调用可以在客户端正确模拟并因此立即执行而无需往返服务器,您可以定义所谓的方法存根(或模拟).

There is another subtle feature in Meteor invalidating what I just said : latency compensation and methods stubs. In case the server method call can be SIMULATED properly in the client and thus executed right away without a round-trip to the server, you can define what is called a method stub (or simulation).

这种行为的一个常见用例是立即在本地(客户端复制子集)数据库中插入一些刚刚发布的用户内容(例如,博客文章下的评论):所有必要的数据和逻辑都可用,这使得感觉模拟服务器端插入.接下来发生的事情是,即使服务器尚未确认这些更改,用户在提交内容后也会立即看到网页已更新.(这是如何在 Meteor 中实现延迟补偿的示例).

A common use case for this behavior is inserting immediately in the local (client side replication subset) database some user content just posted (a comment under a blog article for example) : all the necessary data and logic is available and it makes sense to simulate server side insertion. What happens next is that the user sees the webpage updated as soon as he submitted his content even if the server hasn't acknowledged these changes yet. (this is an example how latency compensation is implemented in Meteor).

当然,服务器对最终插入数据库的内容有最终决定权,这意味着当服务器端孪生方法执行时,它的操作将优先并替换插入到本地数据库中的内容.

Of course the server has final words on what gets ultimately inserted in the database, this means that when the server side twin method is executed, its actions will take precedence and replace what was inserted in the local database.

要定义这样的方法存根,您只需在客户端代码上定义相同的服务器方法名称.如果方法声明是在共享代码中定义的(发送到客户端和服务器),您可以通过检查 isSimulation 属性来测试方法调用是否实际上是一个模拟:

To define such method stub, you just have to define the same server method name on client code. If the method declaration is defined in shared code (shipped both to client and server), you can test if the method call is actually a simulation by checking the isSimulation property :

Meteor.methods({
    myMethod: function(arguments...){
        if(this.isSimulation){
            // called from the client
        }
    }
});

2014 年 11 月 26 日更新:@steph643 评论了我之前答案的最后一部分实际上是错误的,这里是一个更正.

UPDATE 26/11/2014 : @steph643 commented on how the last part of my previous answer was actually wrong, here is a correction.

请注意,服务器上的方法调用始终可以使用同步语法调用,因为服务器环境提供了足够的阻塞机制(纤程).

Note that on the server method calls can always be invoked using the synchronous syntax because server environment provides adequate blocking mechanism (fibers).

然而,在客户端,如果你从一个方法存根返回一些东西,只有当你在另一个存根中并且你可以以同步方式检索结果时,它才能同步执行,即

On the client however, if you return something from a method stub, it can be executed synchronously only if you're inside another stub and you can retrieve the result in a synchronous way, ie

Meteor.methods({
  intermediateMethod: function(){
    return " WORLD";
  },
  method: function(){
    var result = "HELLO";
    result += intermediateResult;
    var intermediateResult = Meteor.call("intermediateMethod");
    return result;
  }
});

考虑到 Mongo 集合操作(​​插入/更新/删除)是作为 Meteor 方法实现的,并且它们的客户端版本正在实现可以同步执行的有效存根(修改 minimongo 复制的本地数据库子集),这种行为有点奇怪.

This behavior is a bit weird considering that Mongo collection operations (insert/update/delete) are implemented as Meteor methods and their client versions are implementing valid stubs (modification of minimongo replicated local database subset) that can be executed synchronously.

这篇关于Meteor.methods 返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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