承诺从我的服务中返回未定义
Posted
技术标签:
【中文标题】承诺从我的服务中返回未定义【英文标题】:Promise return undefined from my service 【发布时间】:2016-08-17 23:03:13 【问题描述】:我使用websql 从 web sql 获取我的数据
我创建了我的控制器、我的模型和我的服务:
服务
app.project.service('ProjectService', ['$webSql', 'config', function ($webSql, config)
var db = $webSql.openDatabase('MyApp', '1.0', 'MyApp', 2 * 1024 * 1024);
this.getAll = function (params)
var projects = [];
db.selectAll("projects").then(function(results)
for(var i=0; i < results.rows.length; i++)
projects.push(results.rows.item(i));
return projects;
)
;
this.create = function (project)
return db.insert('projects', project).then(function(results)
console.log(results.insertId);
);
;
this.createTable = function ()
return db.createTable('projects',
"id":
"type": "INTEGER",
"null": "NOT NULL", // default is "NULL" (if not defined)
"primary": true, // primary
"auto_increment": true // auto increment
,
"created":
"type": "TIMESTAMP",
"null": "NOT NULL",
"default": "CURRENT_TIMESTAMP" // default value
,
"name":
"type": "TEXT",
"null": "NOT NULL"
,
"path":
"type": "TEXT",
"null": "NOT NULL"
,
"file":
"type": "TEXT",
"null": "NOT NULL"
);
;
]);
型号
app.project.factory('Project', function (ProjectService)
var Project = function (properties)
// Model
this.file = null;
this.name = null;
this.path = null;
this.is_active = null;
angular.extend(this, properties);
;
Project.prototype.setModel = function (obj)
angular.extend(this, obj);
;
// With service
// ------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------
Project.prototype.getAll = function ()
return ProjectService.getAll();
;
Project.prototype.create = function (project)
return ProjectService.create(project);
;
Project.prototype.createTable = function ()
return ProjectService.createTable();
;
return Project;
);
控制器
app.home.controller('HomeController', ['$scope', '$http', '$webSql', 'Project', function ($scope, $http, $webSql, Project)
var project = new Project();
$scope.refresh = function()
new_project = "name": 'Project1 WEB PREPROD', "path": 'project1.preprod.site.com', 'file': '/release.txt';
// project.createTable();
project.create(new_project);
$scope.projects = project.getAll();
console.log(project.getAll());
;
$scope.refresh();
]);
我可以 createTable 并创建一个新对象(项目),但是当我执行$scope.projects = project.getAll();
我的控制台日志返回undefined
我在我的服务中尝试过这个
this.getAll = function (params)
var projects = [];
db.selectAll("projects").then(function(results)
for(var i=0; i < results.rows.length; i++)
projects.push(results.rows.item(i));
).then(function()
return projects;
)
;
但我有同样的问题
【问题讨论】:
【参考方案1】:您错过了从服务getAll
方法返回承诺。基本上db.selectAll
函数会返回一个承诺,而您不会返回。
this.getAll = function (params)
//return promise from here.
return db.selectAll("projects").then(function(results)
for(var i=0; i < results.rows.length; i++)
projects.push(results.rows.item(i));
return projects;
)
;
并替换它:
$scope.projects = project.getAll();
通过
project.getAll().then(function(results)
$scope.projects = results;
);
【讨论】:
但我添加了这个 .then(function() return projects; );在我的函数 db.selectAll 之后?我该怎么做? @JérémieChazelle 内部返回只是链接promise
,第一个地方你还没有返回承诺对象..以上是关于承诺从我的服务中返回未定义的主要内容,如果未能解决你的问题,请参考以下文章