nativescript-barcodescanner icon indicating copy to clipboard operation
nativescript-barcodescanner copied to clipboard

Navigation fails

Open HunterTKingsley opened this issue 6 years ago • 5 comments

Hi,

On Android this failed sometimes until Navtivescript 5.4.2 and 3.2.1 which I can report that now this fails every time. On iOS this works consistently. It fails with a parent/child issue on the topmost.navigation().

A button points to onScan, which then calls doScan. As you can see, I deprecated it for Android. <FAB:fab android:visibility="collapsed" id="fabButton" icon="~/images/barcodeImage.png" class="fab-button-bottom" tap="onScan" />

I was allowing a scan from a product browse and a product search page - which immediately went to the cart to select quantities. To work around the issue, I added a scan on the cart page which, of course, eliminated the navigation and the crash.

export async function onScan(args: NavigatedData) { const page = <Page>args.object; let parts: Array<Part> = page.bindingContext.parts; let partNumScanned = await doScan(); let index = parts.findIndex((p) => p.PartNum === partNumScanned); if (index > -1) { console.log(index + " was used."); try { topmost().navigate({ moduleName: "Purchase/Purchase-page", transition: { name: "fade" }, context: { param1: index, param2: "", param3: "" } }); } catch (e) { console.log("However, navigation failed! " + e.message); } } else { console.log("An error, or the item is no longer for sale. " + partNumScanned); alert("An error, or the item is no longer for sale. " + partNumScanned); } } . . . export async function doScan(): Promise { let stuff: string = ""; let bcs: BarcodeScanner = new BarcodeScanner(); await bcs.scan({ formats: "QR_CODE, EAN_13, CODE_128", showFlipCameraButton: false, preferFrontCamera: false, showTorchButton: true, beepOnScan: true, torchOn: false, resultDisplayDuration: 0, openSettingsIfPermissionWasPreviouslyDenied: true // ios only }).then((result) => { console.log("result.text:" + result.text); stuff = result.text; }, (errorMessage) => { console.log("Error when scanning " + errorMessage); stuff = errorMessage; }); console.log("doScan ended: " + stuff); bcs.stop(); bcs = null; return stuff; }

HunterTKingsley avatar Jul 10 '19 14:07 HunterTKingsley

Usually when navigation leads to a crash, try adding a delay before navigating. So a setTimeout(.., 1000) perhaps.

EddyVerbruggen avatar Jul 10 '19 15:07 EddyVerbruggen

I have same issue after scanning the navigation takes 8-10 sec in Android even after adding setTimeout with 1 sec.

DeepikaKochar avatar Jul 12 '19 06:07 DeepikaKochar

I am also facing the same issue of not being able to navigate inside the .then() function after a successful scan.

cbruce11 avatar Jul 16 '19 20:07 cbruce11

I was having the same issue and realized that the "this" keyword had a conflict within the promise and I had to create a new variable to hold to root "this" reference so that I could access the "this.$navigateTo" function. The following worked:

let that = this;

emperorjm avatar Feb 18 '20 19:02 emperorjm

Define : this as that

scan(front) {
let that = this;    
new BarcodeScanner().scan({
....

Then

....
}).then(function(result) {
 that.$navigateTo(somePage, {
 clearHistory: false,
});
...

kakha13 avatar Jun 27 '20 17:06 kakha13