# Accessing stores inside service?

**URL:** <https://discuss.emberjs.com/t/accessing-stores-inside-service/11483>\
**Category:** Ember Data\
**Created:** [August 28, 2016, 5:52pm UTC](https://discuss.emberjs.com/t/accessing-stores-inside-service/11483 "2016-08-28T17:52:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prakash\_Chokalingam](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Prakash\_Chokalingam](https://discuss.emberjs.com/u/Prakash_Chokalingam)\
**Post date:** [August 28, 2016, 5:52pm UTC](https://discuss.emberjs.com/t/accessing-stores-inside-service/11483/1 "2016-08-28T17:52:36Z")

</div>

First of all am new to ember …

I am creating an ember service like the below and wanna use ember data for some functionalities is it possible ?

```
  import Ember from 'ember';
  export default Ember.Service.extend({
  init() {
    this._super(...arguments);
     this.get('store')
    console.log(this.get('store')); //undefined
  }
});

```

How can i access stores inside service ?

---

<div class="post-metadata">

**Author:** ![lightblade](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/lightblade/32/15632_2.png) [@lightblade](https://discuss.emberjs.com/u/lightblade)\
**Post date:** [August 28, 2016, 5:54pm UTC](https://discuss.emberjs.com/t/accessing-stores-inside-service/11483/2 "2016-08-28T17:54:27Z")

</div>

You just needs to inject the store service.

````nohighlight
```js
import Ember from 'ember';

export default Ember.Service.extend({
  store: Ember.inject.service(),

  init() {
    this._super(...arguments);
  
  console.log(this.get('store'));
  }
});

````

---

<div class="post-metadata">

**Author:** ![larry\_karani](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/larry_karani/32/16906_2.png) [@larry\_karani](https://discuss.emberjs.com/u/larry_karani)\
**Post date:** [February 14, 2020, 2:19pm UTC](https://discuss.emberjs.com/t/accessing-stores-inside-service/11483/3 "2020-02-14T14:19:50Z")

</div>

does this apply in the current version of ember js

---

<div class="post-metadata">

**Author:** ![chriskrycho](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/chriskrycho/32/17763_2.png) [@chriskrycho](https://discuss.emberjs.com/u/chriskrycho)\
**Post date:** [February 14, 2020, 2:34pm UTC](https://discuss.emberjs.com/t/accessing-stores-inside-service/11483/4 "2020-02-14T14:34:20Z")

</div>

Yep, it hasn’t changed, except that you can now use native classes, native getters, and much nicer imports! The example above should be written like this today:

```nohighlight
import Service, { inject as service } from '@ember/service';

export default class MyService extends Service {
  @service store;

  constructor() {
    super(...arguments);
    console.log(this.store);
  }
}

```

For details on what all those pieces mean, you likely want to read through [the Ember Guides’ section on services](https://guides.emberjs.com/release/services/). If you aren’t familiar with some of the modern JavaScript concepts (and a _lot_ has changed, so that’s quite reasonable), you may want to read [this section on JavaScript classes](https://guides.emberjs.com/release/getting-started/working-with-html-css-and-javascript/#toc_javascript-classes) as well.

---

<div class="post-metadata">

**Author:** ![larry\_karani](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/larry_karani/32/16906_2.png) [@larry\_karani](https://discuss.emberjs.com/u/larry_karani)\
**Post date:** [February 14, 2020, 2:56pm UTC](https://discuss.emberjs.com/t/accessing-stores-inside-service/11483/5 "2020-02-14T14:56:43Z")

</div>

This is my task.js. I want to access the store on this component everything works fine. Is their anything I can Improve on?

` import Component from ‘@ember/component’; import { inject as service } from ‘@ember/service’;

```
export default Component.extend({

    store: service(),

    actions: {

        updatePin(id) {

            this.store.findRecord('task', id).then(function (task) {

                const {isPined} = task;

                console.log(task)

                // ...after the record has loaded

                task.isPined = !isPined;

            });

    }

}

})`

```

---

<div class="post-metadata">

**Author:** ![chriskrycho](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/chriskrycho/32/17763_2.png) [@chriskrycho](https://discuss.emberjs.com/u/chriskrycho)\
**Post date:** [February 14, 2020, 5:51pm UTC](https://discuss.emberjs.com/t/accessing-stores-inside-service/11483/6 "2020-02-14T17:51:46Z")

</div>

This should work fine; but a few notes:

- `// after the record has loaded`

- Instead of doing `function(task) { ... }` as the callback you can (and generally should, here) use an “arrow function” instead, so that you don’t re-bind `this`:

- You’ll need to actually _save_ the updated `task` when you’re done:

You _may_ find it more convenient to write it using `async`/`await` syntax:

```nohighlight
import Component from '@ember/object';
import Service, { inject as service } from '@ember/service';
import { action } from '@ember/object';

export default Component.extend({
    store: service(),

    actions: {
        async updatePin(id) {
            let task = await this.store.findRecord('task', id);
            const {isPined} = task;
            task.isPined = !isPined;
            task.save();
        }
    }
}

```
