gpt4 book ai didi

javascript - Ionic 应用程序在初始化完成之前开始工作

转载 作者:行者123 更新时间:2023-12-02 22:34:41 29 4
gpt4 key购买 nike

我正在使用 Firebase 实时数据库开发 Ionic 条码扫描仪应用程序。启动时,应用应同步来自 Firebase 的数据。但不知何故,应用程序似乎在初始化/与数据库同步完成之前开始工作。因此用户将看不到任何数据。

经过几次重启后,应用完成了数据同步,用户终于可以看到数据了。请指导我解决这个问题。该应用程序会在用户每次使用该应用程序时同步数据吗?

提前致谢!

tabs.page.ts

import { Component, OnInit } from '@angular/core';
import { BarcodeScanner } from '@ionic-native/barcode-scanner/ngx';
import { DataServiceService } from '../../app/data-service.service';
import { Toast } from '@ionic-native/toast/ngx';
import { Platform, AlertController } from '@ionic/angular';
import * as moment from 'moment';
@Component({
selector: 'app-tabs',
templateUrl: './tabs.page.html',
styleUrls: ['./tabs.page.scss'],
})
export class TabsPage implements OnInit {

productViews: any = {};
productViewsbyUser: any[] = [];
isProdcutsAvailable = true;

selectedProduct: any;
isCameraOpen = false;
showScan = false;
products: any[] = [];
productFound = true;
displayUserName: any;

exitModalDisplayed = false;


constructor(
private barcodeScanner: BarcodeScanner,
private toast: Toast,
public platform: Platform,
public dataService: DataServiceService,
public alertCtrl: AlertController) {
console.log(`Tabs Page called`);
}

ngOnInit() {

this.productHunt();

}

productHunt() {
this.dataService.getProducts()
.subscribe((response) => {
this.products = <any[]><unknown>response;
console.table('products ', this.products);
});
}

getMoment() {
return moment().milliseconds(0);
}

// Start scanning procedure
scan() {
this.selectedProduct = {};
this.isCameraOpen = true;
this.showScan = true;
this.barcodeScanner.scan().then((barcodeData) => {
setTimeout(() => {
this.isCameraOpen = false;
}, 500);
if (barcodeData.cancelled) {
return;
}
console.log(`barcodeData`, barcodeData);
this.selectedProduct = this.products.find(product => product.prodId === barcodeData.text);
if (this.selectedProduct !== undefined) {
this.selectedProduct.scannedAt = this.getMoment().toISOString();
// this.selectedProduct.userName = this.displayUserName(); // TO TEST !!!
this.productFound = true;
// insert product views with firebase generated based key
this.dataService.insertProductViewAnalytics(this.selectedProduct)
.subscribe(() => {
console.log(`Product view analytics inserted in Firebase`);
this.initScanHistoryData();
});
} else {
this.productFound = false;
this.toast.show(`Product not found`, '5000', 'center').subscribe(
toast => {
console.log(toast);
}
);
}
}, (err) => {
setTimeout(() => {
this.isCameraOpen = false;
}, 1000);
this.toast.show(err, '5000', 'center').subscribe(
toast => {
console.log(toast);
}
);
});
}

async initScanHistoryData() {
this.dataService.getProductViewsForUser()
.subscribe((response) => {
this.productViews = response;
const userProductViews = [];
// tslint:disable-next-line: forin
for (const key in this.productViews) {
userProductViews.push(this.productViews[key]);
}
userProductViews.sort(function (a, b) {
return moment(b.scannedAt).diff(moment(a.scannedAt));

// ENTER USER NAME HERE???

});

this.productViewsbyUser = userProductViews;
console.log('user productViews ', userProductViews);

if (this.productViewsbyUser.length) {
this.isProdcutsAvailable = true;
} else {
this.isProdcutsAvailable = false;
}
console.log('productViews ', this.productViews);
});
}
}


data-service.service.ts

import { Component, OnInit } from '@angular/core';
import { BarcodeScanner } from '@ionic-native/barcode-scanner/ngx';
import { DataServiceService } from '../../app/data-service.service';
import { Toast } from '@ionic-native/toast/ngx';
import { Platform, AlertController } from '@ionic/angular';
import * as moment from 'moment';
@Component({
selector: 'app-tabs',
templateUrl: './tabs.page.html',
styleUrls: ['./tabs.page.scss'],
})
export class TabsPage implements OnInit {

productViews: any = {};
productViewsbyUser: any[] = [];
isProdcutsAvailable = true;

selectedProduct: any;
isCameraOpen = false;
showScan = false;
products: any[] = [];
productFound = true;
displayUserName: any;

exitModalDisplayed = false;


constructor(
private barcodeScanner: BarcodeScanner,
private toast: Toast,
public platform: Platform,
public dataService: DataServiceService,
public alertCtrl: AlertController) {
console.log(`Tabs Page called`);
}

ngOnInit() {

this.productHunt();

}

productHunt() {
this.dataService.getProducts()
.subscribe((response) => {
this.products = <any[]><unknown>response;
console.table('products ', this.products);
});
}

getMoment() {
return moment().milliseconds(0);
}

// Start scanning procedure
scan() {
this.selectedProduct = {};
this.isCameraOpen = true;
this.showScan = true;
this.barcodeScanner.scan().then((barcodeData) => {
setTimeout(() => {
this.isCameraOpen = false;
}, 500);
if (barcodeData.cancelled) {
return;
}
console.log(`barcodeData`, barcodeData);
this.selectedProduct = this.products.find(product => product.prodId === barcodeData.text);
if (this.selectedProduct !== undefined) {
this.selectedProduct.scannedAt = this.getMoment().toISOString();
// this.selectedProduct.userName = this.displayUserName(); // TO TEST !!!
this.productFound = true;
// insert product views with firebase generated based key
this.dataService.insertProductViewAnalytics(this.selectedProduct)
.subscribe(() => {
console.log(`Product view analytics inserted in Firebase`);
this.initScanHistoryData();
});
} else {
this.productFound = false;
this.toast.show(`Product not found`, '5000', 'center').subscribe(
toast => {
console.log(toast);
}
);
}
}, (err) => {
setTimeout(() => {
this.isCameraOpen = false;
}, 1000);
this.toast.show(err, '5000', 'center').subscribe(
toast => {
console.log(toast);
}
);
});
}

async initScanHistoryData() {
this.dataService.getProductViewsForUser()
.subscribe((response) => {
this.productViews = response;
const userProductViews = [];
// tslint:disable-next-line: forin
for (const key in this.productViews) {
userProductViews.push(this.productViews[key]);
}
userProductViews.sort(function (a, b) {
return moment(b.scannedAt).diff(moment(a.scannedAt));

// ENTER USER NAME HERE???

});

this.productViewsbyUser = userProductViews;
console.log('user productViews ', userProductViews);

if (this.productViewsbyUser.length) {
this.isProdcutsAvailable = true;
} else {
this.isProdcutsAvailable = false;
}
console.log('productViews ', this.productViews);
});
}
}


最佳答案

看起来您粘贴了两次 Tabs 组件而不是数据服务,但如果您的目标是让它等待,那么在我看来您只是没有在异步函数上添加等待,例如:

async initScanHistoryData() {
await this.dataService.getProductViewsForUser()
...
}
话虽这么说,正如一些评论中提到的,这可能不是最佳实践,为了提供更好的用户体验,您应该允许异步函数“同步”工作,加载页面并显示用户可以通过旋转器或一些反馈来显示数据库仍在获取数据,直到数据从数据库返回。

关于javascript - Ionic 应用程序在初始化完成之前开始工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58777799/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com