0

我的角度控制器如下所示:

angular.module("campos").controller("HomeCtrl", ['$scope', '$meteor', '$rootScope', '$state', '$modal',
    function ($scope, $meteor, $rootScope, $state, $modal) {
    // $scope.users = $meteor.collection(Meteor.users, false).subscribe('users');

    $meteor.autorun($scope, function () {
        $scope.$meteorSubscribe('myOrgnization', 'crown').then(function () {
                $scope.organization.forEach(function (org) {
                    console.log(org._id);
                });
            },
            function (error) {
                console.log(error);
            });
    });

    $scope.organization = $meteor.collection(function(){
        return Organizations.find({});
    });
}

]);

服务器代码如下所示:

Meteor.publish('myOrgnization', function(org){
    return Organizations.find({
        'code' : org
    });
});

客户端控制器正确完成,从控制台我可以正确获取记录 ID。但是,我抛出了一个异常:

 TypeError: undefined is not a function
    at http://localhost:8026/packages/urigo_angular.js?de756130fe1ce5aaa41d3967997c1b2090bcdd1b:390:12
    at Array.forEach (native)
    at Function._.each._.forEach (http://localhost:8026/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:149:11)
    at diffArray (http://localhost:8026/packages/urigo_angular.js?de756130fe1ce5aaa41d3967997c1b2090bcdd1b:388:5)
    at updateCollection (http://localhost:8026/packages/urigo_angular.js?de756130fe1ce5aaa41d3967997c1b2090bcdd1b:1310:3)
    at http://localhost:8026/packages/urigo_angular.js?de756130fe1ce5aaa41d3967997c1b2090bcdd1b:1154:11
    at http://localhost:8026/packages/angular_angular.js?feeaf4750c9fd3ceafa9f712f99a21ee6fef9b41:17819:31
    at completeOutstandingRequest (http://localhost:8026/packages/angular_angular.js?feeaf4750c9fd3ceafa9f712f99a21ee6fef9b41:5529:10)
    at http://localhost:8026/packages/angular_angular.js?feeaf4750c9fd3ceafa9f712f99a21ee6fef9b41:5806:7
home.ng.js:8 dPmZoCeJ9YbKxKA4u

它似乎来自角度延迟,但我认为我正确地遵循了订阅语法?即使我将其更改为使用 $meter.subscribe,它也会引发相同的异常。

任何帮助都感激不尽。

提前致谢。

4

1 回答 1

0

您应该在承诺返回后分配 $scope 集合:

    $scope.$meteorSubscribe('myOrgnization', 'crown').then(function () {
        $scope.organization = $scope.$meteorCollection(function(){
            return Organizations.find({});
        });
    });

我还建议为您的发布参数传递一个对象:

$scope.$meteorSubscribe('myOrginization', {arg1: 'crown'}).then(...)

然后当然在服务器上你会得到:

Meteor.publish('myOrgnization', function(org){
    return Organizations.find({
        'code' : org.arg1
    });
});
于 2015-10-27T22:14:34.923 回答