简体   繁体   中英

$meteor.collection doesn't return a Collection

Following the Angular-Meteor tutorial step 9 , I'm trying to create an Angular directive that uses a Meteor collection.

This file is in the root folder:

TicTacToeBoards = new Meteor.Collection("tic_tac_toe_boards");

if (Meteor.isServer) {
    Meteor.publish('TicTacToeBoards', function() { return TicTacToeBoards.find(); });
}

This file is in the /client folder:

angular.module('TicTacToe').directive('tictactoegraph', function() {
    return {
        templateUrl: 'client/graph/tictactoegraph.ng.html',
        scope: true,
        controller: function($scope, $meteor, Sigma, TicTacToeClass) {
            $scope.TicTacToeBoards = false;

            $meteor.subscribe('TicTacToeBoards').then(function(subscriptionHandle){
                $scope.TicTacToeBoards = $meteor.collection(TicTacToeBoards);
            });
        },
        link: function($scope, element, attrs) {
            // TODO: Ask SO if there's a better way to wait on the subscription....
            $scope.$watch('TicTacToeBoards', function(newValue, oldValue) {
                if ($scope.TicTacToeBoards) {
                    console.log($scope.TicTacToeBoards); // An array of objects.
                    var nextBoards = $scope.TicTacToeBoards.find({ numberOfMoves: 0 });
                }
            });
        }
    }
}

Unfortunately, it gives an error:

TypeError: $scope.TicTacToeBoards.find is not a function

It appears that $scope.TicTacToeBoards is not a Mongo cursor, but is the array of objects that TicTacToeBoards.find() would return. Why isn't it a cursor?

You are right, $meteor.collection doesn't return a cursor, it returns an array of type AngularMeteorCollection which is different: http://angular-meteor.com/api/AngularMeteorCollection

It does that because we wanted to give Angular developers a regular array with ll it's API to work easily with.

It's an interesting idea to add a find function to that array though. Do want want to use that function to return a filtered object? You could use filters for that, but maybe we can add this option as well

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