【问题标题】:Meteor Angular get all usersMeteor Angular 获取所有用户
【发布时间】:2016-11-27 21:53:54
【问题描述】:

我是 Meteor 和 Angular 2 的新手,我正在努力列出我的客户端中的所有用户。据我了解,我必须在我的服务器上发布并在我的客户端上订阅,我这样做了以下方式:

users.ts

import {Meteor} from 'meteor/meteor';

Meteor.publish('usersList', function () {
    console.log('Publish from server ');
    return Meteor.users.find({_id: {$ne: Meteor.userId()}});
});

但是,这甚至没有被调用。我将这个文件导入到我的 ma​​in.ts 中。我不确定我应该如何发布它们以吸引所有用户。

我已通过关注Meteor-Angular2 tutorial 创建了另一个集合。该文件如下: wishs.ts

import {Meteor} from 'meteor/meteor';
import {Wishes} from '../../../both/collections/wishes.collection';

Meteor.publish('wishList', function () {
    console.log("wishlist "+this.userId);
    return Wishes.find(buildQuery.call(this));
});


function buildQuery(): Object {
    const isAvailable = {
        $and: [{
            owner: this.userId
        }, {
            owner: {
                $exists: true
            }
        }]
    }
    return isAvailable;
}

我在服务器的 ma​​in.ts 中调用它,它正在工作。我不知道为什么其中一个文件被调用,而另一个文件为什么不被调用。任何方向将不胜感激!

【问题讨论】:

    标签: angular meteor


    【解决方案1】:

    您的 users.ts 中的流星发布方法将为订阅“userList”的每个新客户端触发其回调。 如果您想获取 Meteor 用户集合中的所有用户,一个简单的 find({}) 将为您提供所有用户,而如果需要任何更具体的查询,您可以在 find 查询中使用查找一个或多个用户.

    从您在帖子中输入的代码 sn-ps 来看,您的代码看起来不错。

    【讨论】:

    • 不幸的是,客户端中的 Meteor.users.find({})(订阅 'userList' 后)只返回当前登录的用户。
    • 如果您创建客户端可访问的用户集合,客户端将从服务器 Meteor.users.find({}) 接收用户,或直接向客户端发送消息。如果您在客户端使用 Meteor.users.find({}),它将返回您当前的用户,因为它不知道其余的 :)。
    猜你喜欢
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 2016-10-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多