AngularJS meteor mongo集合键值

AngularJS meteor mongo collection key value

本文关键字:键值 集合 mongo meteor AngularJS      更新时间:2023-09-26

我有这样的代码

$scope.users = $meteor.collection( function() {
        return AllClients.find({}, {name: 1, _id: 0});
      });

我期望返回一个像这样的值

/* 1 */
{
    "name" : "Samsung"
}
/* 2 */
{
    "name" : "HP"
}

但是它仍然返回一个像这样的值

/* 1 */
{
    "_id" : "SqFP23zTXo6MqDLxP",
    "code" : "A100",
    "name" : "Samsung",
    "address" : "Korea"
}
/* 2 */
{
    "_id" : "8QtNBoBGrvv5wWpuZ",
    "code" : "B100",
    "name" : "HP",
    "address" : "USA"
}

这是一个bug吗?或者糟糕的编码…

首先,如果您不希望其他信息在客户端可用,那么您需要在服务器端完成工作。这是通过publish方法处理的。

首先,删除autoppublish:

> meteor remove autopublish
然后你可以在你的服务器文件夹中创建publish方法:
Meteor.publish('clientNames', function() {
    return AllCients.find({}, {fields: {name: 1} });
});

这个发布方法将找到所有的客户端,只允许name字段,请记住,你可能仍然得到_id字段,我相信它总是发送。

然后在你的客户端你需要订阅它:

$scope.$meteorSubscribe('clientNames').then(function() {
    $scope.users = $scope.$meteorCollection(AllClients, false);
});
使用meteor,当您从客户端访问信息时,您只有可以访问服务器允许您访问的内容。在这种情况下,您可以请求AllCients,但由于服务器不允许,因此无法获得所有信息。