# On page/app load create createRecord based on queryParams array

**URL:** <https://discuss.emberjs.com/t/on-page-app-load-create-createrecord-based-on-queryparams-array/11716>\
**Category:** Ember Data\
**Created:** [October 6, 2016, 9:52am UTC](https://discuss.emberjs.com/t/on-page-app-load-create-createrecord-based-on-queryparams-array/11716 "2016-10-06T09:52:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lasse\_van\_den\_Bosch](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Lasse\_van\_den\_Bosch](https://discuss.emberjs.com/u/Lasse_van_den_Bosch)\
**Post date:** [October 6, 2016, 9:52am UTC](https://discuss.emberjs.com/t/on-page-app-load-create-createrecord-based-on-queryparams-array/11716/1 "2016-10-06T09:52:12Z")

</div>

I’m entirely new to EmberJS. I’ve been fiddling around lately, but find my self in a bit of a jam with a specific issue. I’m still getting grips on the basic concepts of this framework, so bear with me.

Basically I want to manipulate the ember store (only locally) on page/app load. Specifically **an array of queryParams should create a series of new records on page/app load**.

The query could look like: `http://localhost:4200/?post[]=1&post[]=2`

I’m able to fetch the queryParams and loop over the desired query (here _post_) but as soon as i try to access the store from within the forEach loop a console error is thrown back at me: `Cannot read property 'store' of undefined TypeError: Cannot read property 'store' of undefined`

Here’s the index.js for the route:

```
import Ember from 'ember';

export default Ember.Route.extend({

   beforeModel: function(transition){

       console.log(transition.queryParams.post);

       transition.queryParams.post.forEach(function(item){
   			console.log(item);
        // console.log(this.store)
        //
        // this.store.createRecord('post', {
        // title: item
        // })
      })

   },

// manually add record using action 
actions:{
    addpost(){

      console.log(this.store)
      this.store.createRecord('post', {
        title: 'a new post'
      });

    }

  }
});

```

Any ideas on how to go around this? Help much appreaciated! 😄

Here’s the whole app as a repo: [https://github.com/template01/emberparamsstore](https://github.com/template01/emberparamsstore)

All the bests

Lasse

---

<div class="post-metadata">

**Author:** ![nopyronoparty](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@nopyronoparty](https://discuss.emberjs.com/u/nopyronoparty)\
**Post date:** [October 6, 2016, 10:13am UTC](https://discuss.emberjs.com/t/on-page-app-load-create-createrecord-based-on-queryparams-array/11716/2 "2016-10-06T10:13:48Z")

</div>

This looks like a scope problem in your forEach loop.

**var me = this;** transition.queryParams.post.forEach(function(item){ console.log(item); console.log( **me**.store) // // **me**.store.createRecord(‘post’, { // title: item // }) });

The error did not show up in your addPost action.

---

<div class="post-metadata">

**Author:** ![Lasse\_van\_den\_Bosch](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Lasse\_van\_den\_Bosch](https://discuss.emberjs.com/u/Lasse_van_den_Bosch)\
**Post date:** [October 6, 2016, 10:36am UTC](https://discuss.emberjs.com/t/on-page-app-load-create-createrecord-based-on-queryparams-array/11716/3 "2016-10-06T10:36:24Z")

</div>

Aha! Thanks! So I should assume that `this` is not accessible within the loop, but if stored as a global variable, then (tadaa) no problem.😁

---

<div class="post-metadata">

**Author:** ![nopyronoparty](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@nopyronoparty](https://discuss.emberjs.com/u/nopyronoparty)\
**Post date:** [October 6, 2016, 11:10am UTC](https://discuss.emberjs.com/t/on-page-app-load-create-createrecord-based-on-queryparams-array/11716/4 "2016-10-06T11:10:08Z")

</div>

Yes, except that its not a global variable if stored inside the beforeModel function 🙂In this example `me` exists only in your beforeModel function. `me` outside would return `undefined`

---

<div class="post-metadata">

**Author:** ![tjnine](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/tjnine/32/12596_2.png) [@tjnine](https://discuss.emberjs.com/u/tjnine)\
**Post date:** [October 6, 2016, 12:58pm UTC](https://discuss.emberjs.com/t/on-page-app-load-create-createrecord-based-on-queryparams-array/11716/5 "2016-10-06T12:58:08Z")

</div>

No errors in the console, the laravel action works. That’s the only way to persist data right now. I’ve bwen told that I need to MO e the wollTransistion method out of actions because it’s a hook. The console even shows a 200 request to /libraries
