为什么这个未定义

时间:2015-02-03 17:52:20

标签: javascript database angularjs rest variables

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

我的疑问:

            $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);

    };

我的控制台日志返回:

console.log($scope.timesheets);

返回时间表数组

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

但是当我使用console.log(... [0])。sig它返回:

  

TypeError:无法读取属性' sig'未定义的

和console.log(... sig)返回undefined

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

1 个答案:

答案 0 :(得分:1)

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

$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,直到响应从服务器返回。

相关问题