简体   繁体   中英

Cannot perform ember data queries/rest call inside Ember.service

I'm using ember 2.12

Here's my route

import Ember from 'ember';

export default Ember.Route.extend({
    rentalService: Ember.inject.service('rentals-service'),

    model(){
        this.get('rentalService').findRentals();
        console.log('Inside route');
        console.log(this.get('store'));
        return this.get('store').findAll('rental');
  }
});

Here's my ember rental-service

import Ember from 'ember';

export default Ember.Service.extend({
  findRentals() {
    console.log("Inside servce");
    console.log(this.get('store'));
    return "hello from servce";
  }
});

Why is it I cannot access the ember-data on the rental-service? whenever I console .log this.get('store') inside the rental-service it returns an undefined. However on the routes, whenever I console.log the same code it returns this value.

Does this mean I cannot perform a rest call using ember data inside a service?

在此处输入图片说明

I'm using Mirage to mock a web-server/http requests

You cannot access the store inside an ember component/service. A workaround for this is to retrieve the store via dependency injection.

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

  findRentals() {
    console.log("Inside service");
    console.log(this.get('store'));
    return "hello from service";
  }
});

Be wary if you have another service with a name of store , it might fetch that one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM