Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to write service? #48

Open
ivanff opened this issue Sep 9, 2019 · 2 comments
Open

How to write service? #48

ivanff opened this issue Sep 9, 2019 · 2 comments

Comments

@ivanff
Copy link

ivanff commented Sep 9, 2019

I can't get routerExtension inside callback when i write service

component:

`
@component({
selector: 'app-sporttrack',
templateUrl: './sporttrackSearch.component.html',
styleUrls: ['./sporttrack.component.scss']
})
export class SporttrackSearchComponent implements OnInit, OnDestroy {
constructor(public routerExtensions: RouterExtensions, private nfc: SporttrackNfcService, public zone: NgZone) {
}

ngOnInit(): void {
    this.nfc.doStartTagListener.bind(this)
    this.nfc.doStartTagListener(this.searchAthlet).then(() => console.log(this))
}

ngOnDestroy(): void {
    this.nfc.doStopTagListener()
}

searchAthlet(data: NfcTagData) {
    this.routerExtensions.navigate(['sporttrack', athlet.phone])
}

}

`
service

`
@Injectable({
providedIn: 'root'
})
export class SporttrackNfcService {
nfc: Nfc

constructor() {
    this.nfc = new Nfc()
}

doStartTagListener(fn: (data: NfcTagData) => any): Promise<any> {
    return this.nfc.setOnTagDiscoveredListener((data: NfcTagData) => {
        console.dir(data, this.nfc)
        console.log('Tag discovered sporttrack scan! ' + JSON.stringify(data))
        fn(data)
    }).then(() => {
        console.log('OnTagDiscovered Listener set')
    }, (err) => {
        console.log(err)
    })
}

doStopTagListener() {
    this.nfc.setOnTagDiscoveredListener(null).then(() => {
        console.log('OnTagDiscovered nulled')
    }, (err) => {
        console.log(err)
    })
}

}

`

ERROR
`

Tag discovered sporttrack scan! {"id":[4,72,-37,26,-32,95,-127],"techList":["android.nfc.tech.NfcA","android.nfc.tech.MifareUltralight","android.nfc.tech.NdefFormatable"]}
JS: 123
JS: Unhandled Promise rejection: Cannot read property 'routerExtensions' of undefined ; Zone: ; Task: Promise.then ; Value: TypeError: Cannot read property 'routerExtensions' of undefined TypeError: Cannot read property 'routerExtensions' of undefined
JS: at file:///src/app/sporttrack/sporttrack.component.ts:243:21
JS: at file:///node_modules/nativescript-plugin-firebase/firebase.js:2348:51
JS: at Array.map ()
JS: at QuerySnapshot.push.../node_modules/nativescript-plugin-firebase/firebase.js.QuerySnapshot.forEach file:///node_modules/nativescript-plugin-firebase/firebase.js:2348:0
JS: at file:///src/app/sporttrack/sporttrack.component.ts:241:21
JS:

`

@ivanff
Copy link
Author

ivanff commented Sep 11, 2019

ngOnInit(): void { this.zone.runOutsideAngular(() => { this.nfc.doStartTagListener(this.searchAthlet.bind(this)) }) }

doStartTagListener(fn: (data: NfcTagData) => any): Promise<any> { return this.nfc.setOnTagDiscoveredListener((data: NfcTagData) => { console.dir(data, this.nfc) console.log('Tag discovered sporttrack scan! ' + JSON.stringify(data)) this.zone.run(() => fn(data)) }).then(() => { console.log('OnTagDiscovered Listener set') }, (err) => { console.log(err) }) }

@ivanff
Copy link
Author

ivanff commented Sep 11, 2019

I resolve this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant