# Ionic 2 RC0: SQLite errors due to loading timing

**URL:** <https://forum.ionicframework.com/t/ionic-2-rc0-sqlite-errors-due-to-loading-timing/65871>\
**Category:** ionic-v3\
**Created:** [October 5, 2016, 11:08am UTC](https://forum.ionicframework.com/t/ionic-2-rc0-sqlite-errors-due-to-loading-timing/65871 "2016-10-05T11:08:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tdy](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@Tdy](https://forum.ionicframework.com/u/Tdy)\
**Post date:** [October 5, 2016, 11:08am UTC](https://forum.ionicframework.com/t/ionic-2-rc0-sqlite-errors-due-to-loading-timing/65871/1 "2016-10-05T11:08:30Z")

</div>

Hi there,  
I’m not sure if the title is very clear, so I’ll try to explain my problem:

I’m using SQLite to save data in my app. I’ve wrapped it in an `@Injectable` to init DB structure, etc. This service contains the SQLite object connected to my database.  
Database is initialized in `platform.ready()` to make sure the sqlite plugin is correctly loaded. My init function looks like this:

```
public init() {
  return this.db.openDatabase({
     name: 'openscore.db',
     location: 'default' // the location field is required
  }).then(() => {
      return this.initDbStructure();
  });
}

```

This init is working as expected.

In my home page, I’m firing some queries but it always fails with `TypeError: Cannot read property 'executeSql' of undefined`  
The `undefined` is the SQLite object of my service.

I’m pretty sure that the queries are fired before the DB has been initialized. If I add a timeout before executing the queries, everything work as expected. And if I keep using the app, all the future queries will be fine.

Those queries are fired inside a `ionViewDidLoad`. I tried different Lifecycles event, `ngAfterViewInit` but no more success.

This used to work fine on beta 11 with `SQLStorage` (now removed for good reasons, I’ve no problem with that 😉 )

**How can I make sure my components are loaded when my app is totally ready and my DB initialized?**

Thanks everybody! 🙂

---

<div class="post-metadata">

**Author:** ![BogdanC](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@BogdanC](https://forum.ionicframework.com/u/BogdanC)\
**Post date:** [October 5, 2016, 7:00pm UTC](https://forum.ionicframework.com/t/ionic-2-rc0-sqlite-errors-due-to-loading-timing/65871/2 "2016-10-05T19:00:23Z")

</div>

Hello Tdy,

I’m a newbie in ionic land and pretty new in web development (C# desktop apps in the past), but maybe I can help. I came upon your question by chance and I am also working on a database service for my app now. I wanted to give you this small tutorial: [https://www.thepolyglotdeveloper.com/2016/06/working-shared-providers-ionic-2-mobile-app/](https://www.thepolyglotdeveloper.com/2016/06/working-shared-providers-ionic-2-mobile-app/) that I used and it helped me make it work.

I hope that will help you,  
Bogdan

---

<div class="post-metadata">

**Author:** ![richardshergold](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/richardshergold/32/91823_2.png) [@richardshergold](https://forum.ionicframework.com/u/richardshergold)\
**Post date:** [October 5, 2016, 7:52pm UTC](https://forum.ionicframework.com/t/ionic-2-rc0-sqlite-errors-due-to-loading-timing/65871/3 "2016-10-05T19:52:38Z")

</div>

You might want to fire an event once the db is ready and then subscribe to that event in your component?

---

<div class="post-metadata">

**Author:** ![Tdy](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@Tdy](https://forum.ionicframework.com/u/Tdy)\
**Post date:** [October 5, 2016, 10:16pm UTC](https://forum.ionicframework.com/t/ionic-2-rc0-sqlite-errors-due-to-loading-timing/65871/4 "2016-10-05T22:16:14Z")

</div>

Yes I thought about this solution. But, as it’s a fairly common usage so I was wondering if there was a more “built-in” way to do that.  
And I don’t understand what it was working on beta 11 and not anymore on RC0. Maybe using NgModule has an impact on how the components are loaded, I don’t know enough how ngModule actually works to tell.

---

<div class="post-metadata">

**Author:** ![Tdy](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@Tdy](https://forum.ionicframework.com/u/Tdy)\
**Post date:** [October 5, 2016, 10:17pm UTC](https://forum.ionicframework.com/t/ionic-2-rc0-sqlite-errors-due-to-loading-timing/65871/5 "2016-10-05T22:17:22Z")

</div>

Thanks Bogdan. This blog is quite good and really helped me to get my app working. Unfortunately this article won’t help me in my current issue. 😕

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [October 5, 2016, 10:53pm UTC](https://forum.ionicframework.com/t/ionic-2-rc0-sqlite-errors-due-to-loading-timing/65871/6 "2016-10-05T22:53:28Z")

</div>

> [@Tdy](#):
>
> I don’t understand what it was working on beta 11 and not anymore on RC0

I doubt it really was working reliably, you probably just didn’t hit the race condition as frequently. If all the queries are localized within the database service, they are likely returning Promises anyway. Simply make them dependent on something that fires once the database is ready. The end users (components) shouldn’t have to know or care about any of this.

---

<div class="post-metadata">

**Author:** ![richardmarais](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/richardmarais/32/101456_2.png) [@richardmarais](https://forum.ionicframework.com/u/richardmarais)\
**Post date:** [October 17, 2016, 8:31am UTC](https://forum.ionicframework.com/t/ionic-2-rc0-sqlite-errors-due-to-loading-timing/65871/7 "2016-10-17T08:31:29Z")

</div>

I am getting the same issue. Can anyone please advise me how I should structure my code here?

> <https://stackoverflow.com/questions/40079977/javascript-promise-anti-pattern-error>
