lundi 6 juin 2016

get 'store' is undefined in my adapter

I am making a request to a 3rd api through my backend. The api returns an array of events to my frontend and I am having trouble storing it in the ember DS. The route for my API request is eventful and the route/model I am trying to create an event for is eventful-event. I call:

this.get('store').createRecord('eventful-event', concert)

in my eventful adapter and get that 'store' is undefined. Here is my code for my eventful route:

import Ember from 'ember';

export default Ember.Route.extend({
  model () {
    return this.get('store');
  },

  data : {},

  ajax: Ember.inject.service(),

  actions: {
    searchForEvents (data) {
      let eventful = this.get('store').createRecord('eventful', data);
      return eventful.save();
      // .then(() => this.transitionTo('search-results'));
      // return this.get('store').findAll('eventful');
      // let something = this.get('store').findAll('eventful');
      // console.log('something:', something);
      // return eventful.save();
    }
  }
}); 

and my eventful adapter:

import ApplicationAdapter from 'ga-wdi-boston.event-bookmarker/application/adapter';
import Ember from 'ember';

export default ApplicationAdapter.extend({
  ajax: Ember.inject.service(),

  createRecord (store, type, record) {
    let serialized = this.serialize(record, { includeId: true});
    let data = { serialized };
    let dataToSend = {'keywords': record.record.keywords, 'location': record.record.location };
    return this.get('ajax').request('/eventful', {
      method: 'POST',
      data: dataToSend,
    }).then((events) => {
      events['eventful-event'].forEach(function(concert){
        this.get('store').createRecord('eventful-event', concert);
      });
    });
  }
});

This forEach is where the error is thrown. I'm new to ember so I apologize if I am overlooking something simple, or if my approach is not correct. Any advice or help would be much appreciated.

Just put a debugger in. 'This' in the adapter is undefined.




Aucun commentaire:

Enregistrer un commentaire