简体   繁体   中英

AngularJS meteor mongo collection key value

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...

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:

> 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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