MeteorJS - 没有用户系统,如何在客户端过滤数据?

时间:2015-10-01 08:23:48

标签: javascript mongodb meteor

标题可能听起来很奇怪,但我有一个网站会查询Mongo集合中的一些数据。但是,没有用户系统(没有登录等)。每个人都是匿名用户。

问题是我需要根据用户提供的输入文本框查询Mongo集合上的一些数据。因此,我无法使用this.userId插入一行规范,服务器端读取此规范,并将数据发送到客户端。

因此:

// Code ran at the server
if (Meteor.isServer)
{
    Meteor.publish("comments", function ()
    {
        return comments.find();
    });
}

// Code ran at the client
if (Meteor.isClient)
{
    Template.body.helpers
    (
        {
            comments: function ()
            {
                return comments.find()
                // Add code to try to parse out the data that we don't want here
            }
        }
    );
}

在用户端,我可能会根据某些用户输入过滤一些数据。但是,似乎如果我使用return comments.find()服务器将向客户端发送大量数据,那么客户端将承担清理数据的工作。

通过大量数据,不应该多(10,000行),但我们假设有一百万行,我该怎么办?

我对MeteorJS很新,刚刚完成了教程,感谢任何建议!

1 个答案:

答案 0 :(得分:1)

我的建议是阅读文档,特别是the section on Publish and Subscribe

通过将上面的发布函数的签名更改为带参数的签名,您可以在服务器上过滤集合,并将传输的数据限制为所需的数据。

Meteor.publish("comments", function (postId)
{
    return comments.find({post_id: postId});
});

然后在客户端上,您将需要一个传递参数值的订阅调用。

Meteor.subscribe("comments", postId)

确保您已移除autopublish包,否则将忽略此过滤。