呼叫说无法读取 属性 'then of undefined(看起来有效)

Call says cannot read property 'then of undefined (looks valid )

我不知道是不是 React.JS ,或者只是我忘记了一些承诺但我收到了这个错误

const promise = SAIIndexedDB(response.data)
promise.then(function(result){
    this.setState({
         loadingMedications: false                    
    });  
})
.catch(function(error){
    console.log('error', error);
});

错误:

TypeError: Cannot read property 'then' of undefined

行号显示是这一行promise.then(function(result){

我正在通读这篇文章,它似乎是正确的... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

原因 I want/need 一个承诺是 loadingMedications 被设置为 false,当数据完成从函数调用 SAIIndexedDB(..) 加载时关闭微调器

这是调用

的函数
export function SAIIndexedDB(customerData){
    var status = "start in helpers";
    const dbName = "SAIOffline";
    var request = indexedDB.open(dbName, 2);
    request.onerror = function(event) {
    };

    request.onupgradeneeded = function(event) {
        var db = event.target.result;
        var objectStore = db.createObjectStore("medications", { keyPath: "value"});
        objectStore.createIndex("short_description", "short_description", { unique: false });
        //objectStore.createIndex("email", "email", { unique: true });
        objectStore.transaction.oncomplete = function(event) {
            // Store values in the newly created objectStore.
            var customerObjectStore = db.transaction("medications", "readwrite").objectStore("medications");
            customerData.forEach(function(customer) {
              customerObjectStore.add(customer);
            });
            status = "done"
            return status;
        };

    };

}

请像下面这样更新您的代码:

SAIIndexedDB(response.data).then(function(result){
    this.setState({
         loadingMedications: false                    
    });  
})
.catch(function(error){
    console.log('error', error);
});
export function SAIIndexedDB(customerData){
    return new Promise((resolve, reject) => {
    var status = "start in helpers";
    const dbName = "SAIOffline";
    var request = indexedDB.open(dbName, 2);
    request.onerror = function(event) {
           reject(event)
    };

    request.onupgradeneeded = function(event) {
        var db = event.target.result;
        var objectStore = db.createObjectStore("medications", { keyPath: "value"});
        objectStore.createIndex("short_description", "short_description", { unique: false });
        //objectStore.createIndex("email", "email", { unique: true });
        objectStore.transaction.oncomplete = function(event) {
            // Store values in the newly created objectStore.
            var customerObjectStore = db.transaction("medications", "readwrite").objectStore("medications");
            customerData.forEach(function(customer) {
              customerObjectStore.add(customer);
            });
            status = "done"
            resolve(status);
        };

    };
}

}