我正在创建一个应用程序,在自动登录时我需要从本地 sqlite 数据库中检索一些参数。
这是我的身份验证守卫:
export class AutoLoginGuard implements CanLoad {
constructor(private authService: AuthenticationService, private router: Router) {
}
canLoad(): Observable<boolean> {
return this.authService.isAuthenticated.pipe(
filter(val => val !== null), // Filter out initial Behaviour subject value
take(1), // Otherwise the Observable doesn't complete!
map(isAuthenticated => {
console.log('Found previous token, automatic login');
if (isAuthenticated) {
// Directly open inside area
console.log('redirect ');
this.router.navigateByUrl('/nav', {replaceUrl: true});
} else {
// Simply allow access to the login
console.log('not redirect ');
return true;
}
})
);
}
}
在上面的内部调用以下是身份验证服务:
export class AuthenticationService {
// Init with null to filter out the first value in a guard!
isAuthenticated: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(null);
token = '';
constructor(
protected network: Network,
protected userParamsService: UserParametersService
) {
this.loadToken().then(
async (res: any) => {
},
async (res) => {
console.log(res);
this.isAuthenticated.next(false);
}
);
}
async loadToken() {
const token = await Storage.get({key: TOKEN_KEY});
if (token && token.value) {
this.token = token.value;
if (this.network.type == 'none') {
console.log('Auto Login with JWT token');
return this.isAuthenticated.next(true);
} else {
// Authenticated with server
try {
const uuid = await this.getStoredUuid();
await this.userParamsService.getUserParameters(uuid).then(res => {
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.headers.post.Authorization = this.token;
return axios.post('https://www.endpoint.co.uk/applicationApis/auth/autologin.php', {
res
}).then((response) => {
console.log(JSON.stringify(response));
this.isAuthenticated.next(true);
});
});
} catch (e) {
console.log(e);
this.isAuthenticated.next(false);
}
}
} else {
this.isAuthenticated.next(false);
}
}
在上面可以看出我调用了getUserParameters(uuid),这调用了本地sqlite db。但是我在控制台日志中收到以下错误:
2022-01-24 00:51:39.270 25694-25694/io.ionic.starter I/Capacitor/Console: File: http://localhost/main.js - Line 364 - Msg: TypeError: Cannot read properties of undefined (reading 'executeSql')
2022-01-24 00:51:39.271 25694-25694/io.ionic.starter I/Capacitor/Console: File:
2022-01-24 00:51:39.271 25694-25694/io.ionic.starter I/Capacitor/Console: File: http://localhost/main.js - Line 264 - Msg: Found previous token, automatic login
2022-01-24 00:51:39.273 25694-25813/io.ionic.starter V/Capacitor/Plugin: To native (Cordova plugin): callbackId: SQLitePlugin1829806334, service: SQLitePlugin, action: open, actionArgs: [{"name":"global.db","location":"default","dblocation":"nosync"}]
2022-01-24 00:51:39.273 25694-25813/io.ionic.starter V/SQLitePlugin: Android db implementation: built-in android.database.sqlite package
2022-01-24 00:51:39.274 25694-25820/io.ionic.starter V/info: Open sqlite db: /data/user/0/io.ionic.starter/databases/global.db
特别是这部分 http://localhost/main.js - 第 364 行 - Msg: TypeError: Cannot read properties of undefined (reading 'executeSql')
这让我相信调用 getUserParameters 时 sql 服务还没有准备好。
想知道我该如何解决这个问题?
谢谢