简体   繁体   English

在 Meteor.js 上返回多个游标

[英]Returning multiple cursors on Meteor.js

Here is my big point: the users can visit different chatrooms.这是我的重点:用户可以访问不同的聊天室。 When they do so, I push the chatroom ID in a reactive array in the client, if the ID hasn't already been added.当他们这样做时,如果尚未添加 ID,我会将聊天室 ID 推送到客户端的反应数组中。 If it doesn't, I rerun an autorun that shall subscribe to the 50 last messages of every chatrooms the client visited in this session.如果没有,我将重新运行一个自动运行,该自动运行将订阅客户在此会话中访问的每个聊天室的最后 50 条消息。

But I want him to be able to recieve only the 50 last messages for each chatroom !但我希望他只能收到每个聊天室的最后 50 条消息!

Previously I had:以前我有:

Meteor.publish("getChatMess", function(cids) {
    if (!Array.isArray(cids))
        cids = [cids];

    return (ChatMess.find({ chatId: cids[i] }, { sort: { date: -1 }, limit: (75 * cids.length) } ));
});

Which obviously do not fit my needs, so I did that:这显然不符合我的需要,所以我这样做了:

Meteor.publish("getChatMess", function(cids) {
    if (!Array.isArray(cids))
        cids = [cids];


    let i = -1,
        cursors = [];

    while (cids[++i])
    {
        cursors.push(ChatMess.find({ chatId: cids[i] }, { sort: { date: -1 }, limit: 50 } ));
    }

    return (cursors);
});

But there, Meteor throw me an error, as I can't return several cursor from the same collection in a single puslish.但是在那里,Meteor 向我抛出一个错误,因为我不能在一个 puslish 中从同一个集合中返回多个游标。

How can I solve that?我该如何解决? I know I could check for every chat, pick the ids of the 50 last messages (or less) of every chat and query the chatMess collection for all the corresponding ID, but it would be quite heavy and unoptimized for what I want, especially if the client has already visited dozens of chatrooms in the session.我知道我可以检查每个聊天,选择每个聊天的最后 50 条消息(或更少)的 ID,并查询 chatMess 集合以获取所有相应的 ID,但对于我想要的内容来说它会非常繁重且未优化,尤其是如果客户已经在会话中访问了数十个聊天室。

As stated in meteor docs正如流星文档中所述

If you return multiple cursors in an array, they currently must all be from different collections.如果您在一个数组中返回多个游标,它们当前必须都来自不同的集合。 We hope to lift this restriction in a future release.我们希望在未来的版本中取消此限制。

But there is a package called smart-publish which helps you to manage multiple cursors from the same collection.但是有一个名为 smart-publish 的包可以帮助您管理同一集合中的多个游标。 That might help https://atmospherejs.com/mrt/smart-publish这可能会有所帮助https://atmospherejs.com/mrt/smart-publish

Just use只需使用

Meteor.publish("getChatMess", function(cids) {
    if (!Array.isArray(cids)) cids = [cids];
    return ChatMess.find({chatId: {$in: cids}});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM