Return 对模型和服务的承诺

Return a promise with a model and a service

我的承诺有误。

我尝试使用 angular-tree-dnd ,但我对承诺有疑问。

来自我的控制器:

project.getAll().then(function(results) {
    var projects = results;

    $scope.results = [];

    angular.forEach(projects, function(result) {
        $scope.results.push(project.build(result));
    });

    return $scope.results;
});

$scope.tree_data = $TreeDnDConvert.line2tree($scope.results, 'id', 'ParentId');

我的模型:

var Project = function(properties) {
    // Model
    this.description = null;
    this.file = null;
    this.name = null;
    this.ParentId = null;
    this.path = null;

    angular.extend(this, properties);
};

Project.prototype.setModel = function(obj) {
    angular.extend(this, obj);
};

Project.prototype.getAll = function() {
    return ProjectService.getAll();
};

Project.prototype.build = function(data) {
    var project = new Project(data);

    return project;
};

我的服务(使用 $webSql):

this.getAll = function(params) {
    var projects = [];
    return db.selectAll('projects').then(function(results) {
        for (var i = 0; i < results.rows.length; i++) {
            projects.push(results.rows.item(i));
        }
        return projects;
    });
};

我没有错误,但我的家是空的。

我试过了:

project.getAll().then(function(results) {
    var projects = results;

    $scope.results = [];

    angular.forEach(projects, function(result) {
        $scope.results.push(project.build(result));
    });

    return $scope.results;
}).then(function(array) {
    $scope.tree_data = $TreeDnDConvert.line2tree(array, 'id', 'ParentId');
});

但是我有这个错误:

angular.min.js:13550 TypeError: Cannot read property '__children__' of undefined
    at Object._$initConvert.line2tree (ng-tree-dnd.js:1456)

我认为我的数组是空的

晚上好) 请设置一个断点,或使用 debuggeralertconsole.logJSON.stringify(array),在您最后一次操作之前:

$scope.tree_data = $TreeDnDConvert.line2tree(array, 'id', 'ParentId') 

(带有 then 和错误的最新版本)。

我认为你得到了正确的数组,但不是在 promises 中,而是在 line2tree 语法中。

我怀疑这是你问题的根源:

"ParentId":"null"

您树上的两个项目都有一个带有 字符串值 "null" 的 ParentId。这可能意味着 dnd 库正在寻找 ID 为 "null" 的节点,但什么也没找到,并试图访问 undefined 值上的 __children__ 属性。

解决方案:修复数据库中的数据,使父 ID 实际上是空值,而不是值 "null"