【问题标题】:Uncaught TypeError: Cannot read property 'then' of undefined on my function未捕获的类型错误:无法在我的函数上读取未定义的属性“then”
【发布时间】:2023-03-21 07:44:01
【问题描述】:

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

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

当我在“保存数据”onclick 函数中调用 insert_into_Outbox 函数时,我收到“未捕获的类型错误:无法读取未定义的属性 '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)}
  }

【问题讨论】:

  • insert_into_Outbox 不返回承诺,因此它不会有 .then() 函数

标签: javascript progressive-web-apps


【解决方案1】:

如果你想使用insert_into_Outbox 作为一个promise,你应该返回一个promise。试试这样的:

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);
        }
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2015-01-06
    • 2017-07-26
    相关资源
    最近更新 更多