gpt4 book ai didi

javascript - Meteor 方法 - 处理消息时出现内部异常

转载 作者:行者123 更新时间:2023-12-03 10:31:58 25 4
gpt4 key购买 nike

我一直在遵循 Discover Meteor 教程来创建一个禁用自动发布和不安全的应用程序。自动发布一切顺利,但在删除不安全后,我无法在网页上成功显示集合中的帖子。你们能看一下,看看发生了什么事吗?谢谢。

教程: https://www.discovermeteor.com/blog/getting-started-with-meteor/ https://www.discovermeteor.com/blog/meteor-and-security/

代码:

论坛.js

Posts = new Meteor.Collection("posts");

if (Meteor.isClient) {
Meteor.subscribe("posts", "published");
Template.posts.helpers({
posts: function() {
Meteor.call("viewPosts","published");
}
});
}

if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
Meteor.publish("posts", function(status) {
return Posts.find({status: status});
});
Meteor.methods({
viewPosts: function(status) {
return Posts.find({status: status}).fetch(); //This is still problematic
}
});
Posts.allow({
insert: function() {
return true;
}
})
}

论坛.html

<head>
<title>Forum</title>
</head>

<body>
{{> posts}}
</body>

<template name="posts">
<h1>Posts</h1>
<ul>{{#each posts}}
<li>{{name}}</li>
{{/each}}</ul>
</template>

错误消息。无论我从 viewPosts 方法中添加还是删除 .fetch() ,都会出现这种情况:

=> Meteor 服务器已重新启动
I20150320-14:27:09.418(0)?处理消息时发生内部异常 { msg: 'method',
I20150320-14:27:09.419(0)?方法:'viewPosts',
I20150320-14:27:09.419(0)?参数:['已发布'],
I20150320-14:27:09.419(0)? id: '1' } 最大调用堆栈大小超出未定义

最佳答案

问题出在客户端代码中的这个 block :

Template.posts.helpers({
posts: function() {
Meteor.call("viewPosts","published");
}
});

Meteor 在客户端为您检索数据的主要方式是通过其发布/订阅架构,您已经具备该架构并且看起来不错。这意味着您需要显示的帖子已经在客户端上为您提供了,您不需要对服务器执行 Meteor.call() 来获取它们。相反,你的助手可以看起来像这样:

Template.posts.helpers({
posts: function(){
return Posts.find({status: "published"});
}
});

通常,您希望使用 Meteor.call() 来响应模板事件,例如在 Template.posts.events({}) 中。服务器调用通常不属于助手,尽管我猜可能会有异常(exception)。有关更多信息,请参阅“劳累过度的助手”下的这篇优秀文章:https://dweldon.silvrback.com/common-mistakes

关于javascript - Meteor 方法 - 处理消息时出现内部异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29169600/

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