Uncaught TypeError:无法读取我的函数中未定义的属性“ then”

时间:2019-05-13 12:49:36

标签: javascript progressive-web-apps

我正在编写一个功能,用于在互联网连接失败时保存用户提供的数据

我有两个功能,第一个我名为insert_into_Outbox,它可以保存到我的indexedDB临时文件,并在Internet连接断开时在我的serviceworker中注册一个同步事件。当网络立即恢复时,sync事件将运行并将数据保存在数据库中,另一个单击功能将运行“保存数据”过程。

当我在“保存数据” onclick函数中调用insert_into_Outbox函数时,我收到“未捕获的TypeError:无法读取未定义的属性'then'”错误响应。...如何获取它以响应一个诺言??


//Code to run the entire process of saving to indexedDB and create sync event
$(document).on('click','#save', function(_event){
        var item_name = []; var item_code = []; var item_desc = []; var item_price = [];

         $('.item_name').each(function(){ item_name.push( $(this).text()); });
         $('.item_code').each(function(){ item_code.push( $(this).text()); });
         $('.item_desc').each(function(){ item_desc.push( $(this).text()); });
         $('.item_price').each(function(){ item_price.push( $(this).text()); });
        var data = {method:'post',body:JSON.stringify({'cmd':'insert','item_name':item_name,'item_code':item_code, 'item_desc':item_desc, 'item_price':item_price})};
    _event.waitUntil(
         insert_into_Outbox('MyTestDatabase',data).then(() => {return navigator.serviceWorker.ready;})
           .then(reg => reg.sync.register('save-items'))
           .then(() => display_on_screen(obj = {'item_name':item_name,'item_code':item_code, 'item_desc':item_desc, 'item_price':item_price}))
            )
            .catch(() => insert_into_database(data)); 
});

//insert_into_outbox function
insert_into_Outbox = function(store_name,data){try{       
         //Adding to the Database
        var store = getObjectStore(store_name,'readwrite'); var req;
            try {
                req = store.add(data);
            }catch (e){console.log('error inserting into store: '+e.message);}
                req.onsuccess = function (evt) {
                    console.log("Insertion in DB successful");
                };
                req.onerror = function() {
                    console.log("Insertion in DB Failed ", this.error);
                };    
      }catch(e){console.log('error inserting into indexDB store: '+e.message)}
  }

1 个答案:

答案 0 :(得分:1)

如果您想将insert_into_Outbox用作承诺,则应返回一个承诺。尝试这样的事情:

insert_into_Outbox = function(store_name,data) {
    return new Promise((resolve, reject) => {
        try {       
             //Adding to the Database
            var store = getObjectStore(store_name,'readwrite');
            var req;
                try {
                    req = store.add(data);

                } catch (e) {
                    console.log('error inserting into store: '+e.message);
                    reject(e);
                }
                    req.onsuccess = function (evt) {
                        console.log("Insertion in DB successful");
                        resolve(evt);
                    };
                    req.onerror = function() {
                        console.log("Insertion in DB Failed ", this.error);
                        reject(this.error);
                    };    
        } catch(e) {
             console.log('error inserting into indexDB store: '+e.message);
             reject(e);
        }
    }
});