简体   繁体   English

未捕获(承诺)TypeError:这是未定义的

[英]Uncaught (in promise) TypeError: this is undefined

When I call deleteCategory in View Class it gives me an error.当我在视图 Class 中调用 deleteCategory 时,它给我一个错误。 The error in calling del(event.target.id) inside deleteCategory.在 deleteCategory 中调用 del(event.target.id) 的错误。 It worked when I called it this way directly from view Class当我直接从视图 Class 中以这种方式调用它时,它起作用了

       deleteCategory(service){
        this.tbody.addEventListener('click',async  event => {
            const clicked=event.target.tagName.toLowerCase();
            if (clicked == 'a'){
               const a= await this.service.deleteCategory(service.deleteCategory(event.target.id);
            }
        })
       }

Is there is a way to call the deleteCategory in Controller Class using the event handler inside View Class??有没有办法使用视图 Class 中的事件处理程序调用 Controller Class 中的 deleteCategory?


class View{

    constructor(){
        this.e=document.createElement.bind(document);
        this.root=document.getElementById('root');
        this.tbody=document.querySelector('[table-items]');
    }
  
    categorylist(lists){ 
        
        lists.forEach((list) => {
        const tr=createTableRow(this.tbody,"");
        const td=createTableData(tr,list.name,"pl-4");
        const tdEditDelete=createTableData(tr,"","text-right pr-4");
        const editButton=createButton(tdEditDelete,'submit','Edit',list.id,'btn btn-primary mr-2');
        const deleteButton=createA(tdEditDelete,'Delete',list.id,'btn btn-danger');
        this.deleteButton=deleteButton;
        })  
    }



       deleteCategory(del){
        this.tbody.addEventListener('click',  event => {
            const clicked=event.target.tagName.toLowerCase();
            if (clicked == 'a'){     
                del(event.target.id);     
            }
        })
    }
}
class Controller{

    constructor(service,view){
        this.view=view;
        this.service=service;
        this.displayCategoryList();
        this.view.deleteCategory(this.deleteCategory);
        //this.deleteCategory();
    }

    async displayCategoryList() {
        const lists=await this.service.getLists();
        this.lists=lists;
        this.view.categorylist(lists);
    }
    
     async deleteCategory(id){
        //console.log(id);
        
        const a= await this.service.deleteCategory(id);
    }

}

I found this solution and worked for me.我找到了这个解决方案并为我工作。

Instead of calling deleteCategory constructor in Controller Class而不是在 Controller Class 中调用 deleteCategory 构造函数

this.view.deleteCategory(this.deleteCategory);

Replaced it with this换成这个

this.view.deleteCategory((async(id)=> {
     const a= await this.service.deleteCategory(id);
     }));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:undefined不是一个承诺 - Uncaught TypeError: undefined is not a promise Uncaught (in promise) TypeError: result.main is undefined - Uncaught (in promise) TypeError: result.main is undefined barba.js未被捕获的TypeError:无法读取未定义的属性“ Promise” - barba.js Uncaught TypeError: Cannot read property 'Promise' of undefined 未捕获(承诺):类型错误:无法读取 ionic 2 应用程序中未定义的属性“应用” - Uncaught (in promise): TypeError: Cannot read property 'apply' of undefined in ionic 2 app React Redux - Uncaught(in promise)TypeError:无法读取未定义的属性'props' - React Redux - Uncaught (in promise) TypeError: Cannot read property 'props' of undefined 未捕获(承诺)TypeError:无法读取未定义的属性“ forEach” - Uncaught (in promise) TypeError: Cannot read property 'forEach' of undefined Uncaught (in promise) TypeError: listing.owner is undefined 尝试提交时 - Uncaught (in promise) TypeError: listing.owner is undefined when trying to submit 错误 - 未捕获(承诺)类型错误:无法读取未定义的属性“数据” - Error - Uncaught (in promise) TypeError: Cannot read property 'data' of undefined Axios:未捕获(承诺)TypeError:无法读取未定义的属性“协议” - Axios: Uncaught (in promise) TypeError: Cannot read property 'protocol' of undefined 未捕获(在promise中)TypeError:无法读取promiseKey.then上未定义的属性“ length” - Uncaught (in promise) TypeError: Cannot read property 'length' of undefined at promiseKey.then
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM