出版物中有多个游标的错误?

时间:2016-02-22 20:34:02

标签: meteor

我正在编写通知系统(如facebook通知)。所以我有一个通知集合,每个文档都有一个' actorId'存储调用该通知的用户的_id的字段。我想发布最新的通知和演员'单个出版物中的那些通知的信息。所以这是我的发布功能:

Meteor.publish("myNotifications", function () {
    let notificationCursor = Notifications.find({receiver: this.userId});

    // get an array of actorIds, so we can fetch users' info in a single query
    let actorIds = [];
    notificationCursor.forEach(function(notification) {
        actorIds.push(notification.actor);
    });

    return [
        notificationCursor,
        Meteor.users.find({_id: { $in: actorIds }})
    ];
});

我使用React。这是我的组成部分:

NotificationBlock = React.createClass({
    mixins: [ReactMeteorData],
    getMeteorData() {
        let data = {
            notifications: []
        };
        let handle = Meteor.subscribe('myNotifications');
        if (handle.ready()) {
            data.notifications = Notifications.find({}).fetch();
        }
        return data;
    },
    renderNotifications() {
        let list = [];
        _.each(this.data.notifications, notification => {
            let actor = Meteor.users.findOne(notification.actorId);
            list.push(
                <li key={notification._id}>
                    {actor.profile.name} did something...
                </li>
            );
        });
        return list;
    },
    render() {
        return (
            <ul>
                {this.renderNotifications()}
            </ul>
        );
    }
});

问题是,当有新通知时,似乎只有通知才会通过“我的通知”发布。出版物。新通知的演员信息无法通过。所以客户端控制台显示错误,说“演员&#39;在反应组件的这一行是未定义的:

{actor.profile.name} did something...

但是,如果我刷新浏览器,新通知会正确显示(带有演员的信息),而控制台完全没有任何错误!

我的猜测是,在单个出版物中发布多个游标时,只有那些已经添加&#39;,&#39;更改&#39;&#39;删除&#39;事件更新了,对吗?这就是为什么&#34; Meteor.users.find({_ id:{$ in:actorIds}})&#34;不包括新的用户信息,尽管它的参数已被更改。

1 个答案:

答案 0 :(得分:1)

此类集合加入问题的经典解决方案是使用reywood:publish-composite包。这样你的出版物就会变成:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragment2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view_android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true" 
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</RelativeLayout>

还请注意users集合返回的字段的限制。您真的不想为每个其他用户返回整个用户对象。

相关问题