gpt4 book ai didi

javascript - 获取 TypeError : Cannot call method 'then' of undefined in angularjs controller

转载 作者:行者123 更新时间:2023-11-29 22:08:04 26 4
gpt4 key购买 nike

我不断收到错误消息 - 类型错误:当我在我的工厂中调用方法 (database.readOnlyStock()) 时,无法调用未定义的方法“then”。我还觉得有点尴尬的是,该函数确实会相应地运行,因为我看到了 console.log 消息。

这里是工厂代码:

        stocks.factory('database', ['$resource', '$http', '$q', function($resource, $http, $q) {
var dataMethods = {
createConnection: function() {
/*moved to onload below */
},
addStock: function(stock, nameofcompany) {

var _this = this;
console.log("About to add stock");

//Get a transaction
//default for OS list is all, default for type is read
var transaction = this.db.transaction(["portfolio"],"readwrite");
//Ask for the objectStore
var store = transaction.objectStore("portfolio");

//Define a person
var stockTemplate = {
company_name: nameofcompany,
name:stock,
created:new Date()
}

//Perform the add
var request = store.add(stockTemplate);

request.onerror = function(e) {
console.log("failed to add stock to portflio",e.target.error.name);
//some type of error handler
}

request.onsuccess = function(e) {
console.log("successfully added stock to portfolio");
//console.log(store);
//_this.readOnlyStock();

}


},
readOnlyStock: function() {
var deferred = $q.defer();
var transaction = this.db.transaction(["portfolio"],"readonly");
var store = transaction.objectStore("portfolio");

// var cursorRequest = store.openCursor();

var arrayOfStocks = [];




var keyRange = IDBKeyRange.lowerBound(0);
var cursorRequest = store.openCursor(keyRange);
cursorRequest.onsuccess = function(e) {

var cursor = e.target.result;
if(cursor){

arrayOfStocks.push(cursor.value);
cursor.continue();
}
else{


console.log(arrayOfStocks);
console.log('done!');

deferred.resolve(arrayOfStocks);
return deferred.promise;


//return arrayOfStocks;
}
}
cursorRequest.onerror = function(){

console.log('could not fetch data');
}


},




deleteStock: function() {}
}
//return dataMethods;
if (window.indexedDB) {
console.log('indexeddb is supported');
var openRequest = indexedDB.open("users_stocks", 4);
openRequest.onupgradeneeded = function(e) {
var thisDB = e.target.result;
if (!thisDB.objectStoreNames.contains("portfolio")) {
console.log('created object store');
thisDB.createObjectStore("portfolio", {
autoIncrement: true
});
}
}
openRequest.onsuccess = function(e) {
console.log('connection opened');
dataMethods.db = e.target.result
}
openRequest.onerror = function(e) {
console.log('could not open connection');
}
return dataMethods;
}
else {
console.log('indexedDB not supported');

}
}]);

然后这是我的 Controller 代码:

        stocks.controller('portfolio', ['$scope', '$http', 'stockData', 'database', function portfolio($scope, $http, stockData, database) {


$scope.getAllStocks = function(){
console.log('running getll stocks');

database.readOnlyStock().then(function(result) {
console.log('done');

$scope.allstuff = result;
}, function(){

console.log('no');
});


}


}]);

不确定问题出在哪里。

最佳答案

您需要从 readOnluStock 方法返回 promise ,而不是从其中的成功处理程序返回

    readOnlyStock: function () {
var deferred = $q.defer();
var transaction = this.db.transaction(["portfolio"], "readonly");
var store = transaction.objectStore("portfolio");

// var cursorRequest = store.openCursor();

var arrayOfStocks = [];




var keyRange = IDBKeyRange.lowerBound(0);
var cursorRequest = store.openCursor(keyRange);
cursorRequest.onsuccess = function (e) {

var cursor = e.target.result;
if (cursor) {

arrayOfStocks.push(cursor.value);
cursor.
continue ();
} else {


console.log(arrayOfStocks);
console.log('done!');

deferred.resolve(arrayOfStocks);
//return arrayOfStocks;
}
}
cursorRequest.onerror = function () {

console.log('could not fetch data');
}

// the return should be here
return deferred.promise;
},

关于javascript - 获取 TypeError : Cannot call method 'then' of undefined in angularjs controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19781505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com