【发布时间】:2017-07-08 17:05:21
【问题描述】:
我正在使用环回读取客户列表/数组。我将客户数组存储在 $scope.customers.customerList 中。当我第一次登录时,数组看起来很好,但是当我得到它的长度时,它似乎丢失了,因为它返回 0。为什么会发生这种情况?
这是我的代码:
$scope.customers = { customerList: [] };
var filter = {
include: {
relation: 'orders'
}
};
$scope.customers.customerList = Customers.find({filter: filter});
console.log($scope.customers.customerList); //outputs array with length 21
console.log($scope.customers.customerList.length); //array length is 0
【问题讨论】:
-
似乎 0 是一个有效值,并且根据您的代码当然可能,例如如果过滤器产生零结果。
-
在调用
console.log()之后,可能正在修改数组。开发工具的控制台会显示您查看时的值——“console.log() shows the changed value of a variable before the value actually changes”。length不会做同样的事情,因为从中读取的数字是不可变的。你可以试试console.dir()。
标签: javascript arrays node.js loopback