javascript - Logic Not Working After Moving to Service Layer in Angular 2 App -
i have functionality in angular 2 app i'd move service layer, because several components use exact same functionality (and code repeated). far, however, i'm having trouble getting functionality work when move service layer. work when called directly component layer.
first, here's functionality in component. i'm taking in filter choices made user, , filtering observable call based on these filter selections:
body: = {'group': 'consulting'}; private processtype(name: string, value: any, body: any) { if (this.body[name] && !value) { delete this.body[name]; } else { this.body[name] = { $in: value }; } } private onfilterreceived(value: any, type: string, body) { if (type === 'lan') { this.processtype('languages.primary', value, body); } if (type === 'zip') { this.processtype('addresses.zipcode', value, body); } this.filtersservice.getbyfilter(this.page, this.pagesize, this.body, this.sort) .subscribe(resrecordsdata => { this.records = resrecordsdata; this.data = resrecordsdata.data; if (this.filter === undefined) { this.data = resrecordsdata.data; } else this.data.sort(this.filter); }, responserecordserror => this.errormsg = responserecordserror); }
all of above works expected.
however, mentioned, i'd move initial part of logic service layer, , pass result of subscription in component. have tried (that's not working currently) this:
in service layer have this:
public processtype(name: string, value: any, body: any) { if (this.body[name] && !value) { return delete this.body[name]; } else { return this.body[name] = { $in: value }; } } public getfilterinput(value, type, body) { if (type === 'lan') { return this.processtype('languages.primary', value, body); } if (type === 'zip') { return this.processtype('addresses.zipcode', value, body); } }
notice i'm returning values here, because understanding need when abstracting out service layer.
so in component i've refactored this:
body: = {'group': 'consulting'}; private processtype(name, value, body) { this.filtersservice.processtype(name, value, body); } private onfilterreceived(value: any, type: string, sort?) { this.filtersservice.getfilterinput(type, value); this.filtersservice.getbyfilter(this.page, this.pagesize, this.body, this.sort) .subscribe(resrecordsdata => { this.records = resrecordsdata; this.data = resrecordsdata.data; if (this.filter === undefined) { this.data = resrecordsdata.data; } else this.data.sort(this.filter); }, responserecordserror => this.errormsg = responserecordserror); }
and @ present error in console:
inline template:2:8 caused by: cannot read property 'languages.primary' of undefined
what missing here? how need handle differently when moving logic service layer?
edit: made edits code (reflected above) pass in "body". still getting undefined error, however.
this.body[name]
body unavailable in service reason langauges.primary
undefined need pass in body service
update
public processtype(name: string, value: any, body: any) { let body = body; if (body[name] && !value) { return delete body[name]; } else { return body[name] = { $in: value }; } }
and in component, this:
private processtype(name, value) { let body = {'group': 'consulting'}; this.filtersservice.processtype(name, value, body); }
Comments
Post a Comment