为什么这是未定义的

Why is this undefined

为什么这个无效/未定义??

我的查询:

            $scope.timesheets = Sign.query({
            projectId: $scope.selected.project[0],
            startWeek: this.weekStart,
            endWeek: this.weekEnd
        });

        console.log($scope.timesheets);

        console.log($scope.timesheets.sig);

        console.log($scope.timesheets[0].sig);

    };

我的控制台日志 returns:

console.log($scope.timesheets);

returns 时间表数组

[
    {
        "_id": "54d104718c54c8202654d6cd",
        "__v": 0,
        "sig": "ass...", 
......

但是当我使用 console.log(...[0]).sig 时 returns:

TypeError: Cannot read property 'sig' of undefined

和console.log(...sig) returns未定义

为什么我无法获得时间表[0].sig?

看起来你的 Sign 对象是一个 $resource,这意味着 $scope.timesheets 是一个资源对象,包含一个承诺,当它从服务器 returns 时将被解析。试试这个:

$scope.timesheets.$promise.then(function (timesheets) {
    console.log(timesheets[0].sig);
});

Sign.query({ /* ... props ... */}, function (timesheets) {
    console.log(timesheets[0].sig);
});

您能够使用 console.log($scope.timesheets) 的原因是它正在记录对对象的引用,当您在控制台中查看它时,该对象已解决。但是,尝试记录对象的属性将解析为 undefined,直到响应从服务器返回。