nativescript-barcodescanner
nativescript-barcodescanner copied to clipboard
Navigation fails
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
Usually when navigation leads to a crash, try adding a delay before navigating. So a setTimeout(.., 1000) perhaps.
I have same issue after scanning the navigation takes 8-10 sec in Android even after adding setTimeout with 1 sec.
I am also facing the same issue of not being able to navigate inside the .then() function after a successful scan.
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;
Define : this as that
scan(front) {
let that = this;
new BarcodeScanner().scan({
....
Then
....
}).then(function(result) {
that.$navigateTo(somePage, {
clearHistory: false,
});
...