I have this code
我有这个代码
$scope.users = $meteor.collection( function() {
return AllClients.find({}, {name: 1, _id: 0});
});
I'm expecting to return a value like this
我期待返回这样的值
/* 1 */
{
"name" : "Samsung"
}
/* 2 */
{
"name" : "HP"
}
but it still returns a value like this
但它仍然返回这样的值
/* 1 */
{
"_id" : "SqFP23zTXo6MqDLxP",
"code" : "A100",
"name" : "Samsung",
"address" : "Korea"
}
/* 2 */
{
"_id" : "8QtNBoBGrvv5wWpuZ",
"code" : "B100",
"name" : "HP",
"address" : "USA"
}
Is this a bug? Or bad coding...
这是一个错误吗?或者编码不好......
1 个解决方案
#1
0
First off, if you don't want the other bits of information available on the client side, then you need to do the work on the server side. This is handled using publish methods.
首先,如果您不希望客户端提供其他信息,那么您需要在服务器端进行工作。这是使用发布方法处理的。
Before all, remove autopublish:
首先,删除autopublish:
> meteor remove autopublish
Then you can create publish method in your server folder:
然后,您可以在服务器文件夹中创建发布方法:
Meteor.publish('clientNames', function() {
return AllCients.find({}, {fields: {name: 1} });
});
This publish method will find all of the clients and only allow the name field, keep in mind that you may still get the _id field, I believe it's always sent.
这个发布方法将找到所有客户端并且只允许名称字段,请记住,您仍然可以获得_id字段,我相信它总是被发送。
Then on your client side you'll need to subscribe to it:
然后在您的客户端,您需要订阅它:
$scope.$meteorSubscribe('clientNames').then(function() {
$scope.users = $scope.$meteorCollection(AllClients, false);
});
With meteor, when you access information form the client side, you only have access to what the server has allowed you to. In this case, you can request AllCients
and not have all of the information because the server doesn't allow it.
使用meteor,当您从客户端访问信息时,您只能访问服务器允许您访问的内容。在这种情况下,您可以请求AllCients而不是拥有所有信息,因为服务器不允许它。
#1
0
First off, if you don't want the other bits of information available on the client side, then you need to do the work on the server side. This is handled using publish methods.
首先,如果您不希望客户端提供其他信息,那么您需要在服务器端进行工作。这是使用发布方法处理的。
Before all, remove autopublish:
首先,删除autopublish:
> meteor remove autopublish
Then you can create publish method in your server folder:
然后,您可以在服务器文件夹中创建发布方法:
Meteor.publish('clientNames', function() {
return AllCients.find({}, {fields: {name: 1} });
});
This publish method will find all of the clients and only allow the name field, keep in mind that you may still get the _id field, I believe it's always sent.
这个发布方法将找到所有客户端并且只允许名称字段,请记住,您仍然可以获得_id字段,我相信它总是被发送。
Then on your client side you'll need to subscribe to it:
然后在您的客户端,您需要订阅它:
$scope.$meteorSubscribe('clientNames').then(function() {
$scope.users = $scope.$meteorCollection(AllClients, false);
});
With meteor, when you access information form the client side, you only have access to what the server has allowed you to. In this case, you can request AllCients
and not have all of the information because the server doesn't allow it.
使用meteor,当您从客户端访问信息时,您只能访问服务器允许您访问的内容。在这种情况下,您可以请求AllCients而不是拥有所有信息,因为服务器不允许它。