gpt4 book ai didi

javascript - Meteor.call (Meteor.methods) 似乎在客户端和服务器上运行——导致问题

转载 作者:行者123 更新时间:2023-11-29 16:10:19 26 4
gpt4 key购买 nike

我从客户端执行 Meteor.callMeteor.methods 方法,当我 console.log 时,它们被记录下来在命令提示符和浏览器控制台中。

问题在于它实际上似乎是在客户端执行的——它无法访问适当的实体。虽然我在命令提示符下没有收到任何错误,但客户端显示的内容如下:

Exception while simulating the effect of invoking 'createInvite' Meteor.makeErrorType.errorClass {error: "not-found", reason: "evaluator_invite__entity_not_found", details: undefined, message: "evaluator_invite__entity_not_found [not-found]", errorType: "Meteor.Error"…} Error: evaluator_invite__entity_not_found [not-found] at Meteor.methods.createInvite (http://localhost:3000/lib/collections/invites.js?505cdea882e0f829d521280c2057403ec134b075:38:15)

它实际上是在客户端运行的吗?应该有这个错误吗?

最佳答案

如果全局定义,Meteor 方法应该在两种环境中运行,这允许一个很好的特性,称为延迟补偿。

整个延迟补偿概念是题外话,但基本上它意味着模拟客户端服务器实际会做什么,比如立即将文档插入数据库以设计流畅的用户体验。您实质上是在服务器行为发生之前预测服务器行为,从而通过消除网络延迟让您的客户端体验超快响应。

这方面的一个例子可能是在用户提交评论后立即在数据库中插入评论。在这种情况下,在服务器和客户端上执行的 Meteor 方法调用将共享相同的代码。

例如,在设计负责发送电子邮件的方法时,有时提供仅客户端模拟(或“ stub ”)是完全没有意义的。

有时共享部分代码是有意义的,但您需要访问特定于环境的对象:例如,插入评论的方法可能会在服务器上使用 Email.send 来通知作者在他的帖子中添加了评论。

Meteor.methods({
insertComment: function(comment){
check(comment, [...]);
if(! this.isSimulation){
Email.send([...]);
}
return Comments.insert(comment);
}
});

最终,您必须根据您的方法的行为方式来构建不同的代码。

  • 对于仅限服务器的方法,将它们定义在 server 目录下,没有对应的客户端。

  • 对于可以在两个环境中共享完全相同代码的共享方法,只需全局定义它们即可。

  • 对于略有不同的共享方法,您可以使用 Meteor.isClient/Meteor.isServer 或仅方法属性 isSimulation检查当前环境并相应地执行特定代码。

  • 对于几乎不共享代码的共享方法,我个人使用一种模式,在该模式中,我在客户端和服务器环境中定义 Meteor 方法,并处理共享行为,例如检查参数有效性,然后我调用负责的函数实际实现正确的行为。

lib/method.js

Meteor.method({
method: function(args){
check(args, [...]);
//
return implementation(args);
}
});

诀窍是在客户端和服务器上分别定义实现,根据上下文,将调用正确的函数。

client/method.js

implementation = function(args){
// define your client simulation / stub
};

server/method.js

implementation = function(args){
// define your server implementation
};

关于javascript - Meteor.call (Meteor.methods) 似乎在客户端和服务器上运行——导致问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30367831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com