vendredi 7 avril 2017

How does Ember Data write over instances of stored model data

Ok, so I'm new to Ember so bear with me.

I have an Ember application that is communicating with an API that does not adhere to JSONAPI standards, thus I have begun writing my own serializers in order to use Ember Data. However I am finding that when I make multiple requests to the same resource, the data is having trouble writing to the store. Consecutive requests to the same resource always responds with the following error:

TypeError: Cannot convert object to primitive value

Which from my limited understanding, implies that the data I am sending to the store is being treated like a string.

In my Application route I have written a findAll to my model 'listing-item' like so:

model: function() {
  return this.store.findAll('listing-item');
},

In a nested 'user' route, when I do any type of request for the listing-item data that returns an array response (query, findAll) for the listing-item data, I get:

TypeError: Cannot convert object to primitive value

at EmptyObject.SETTER_FUNCTION [as title] (ember.debug.js:20672)
at assign (<anonymous>)
at InternalModel.setupData (internal-model.js:244)
at Class._load (store.js:1728)
at Class._pushInternalModel (store.js:2055)
at Class._push (store.js:1995)
at finders.js:141
at Backburner.run (ember.debug.js:720)
at Class._adapterRun (store.js:2253)
at finders.js:139

(Title is a field in my listing item model).

As I mentioned earlier, my API does not adhere to JSONAPI standards, so I've written a listing-item serializer like so:

import DS from 'ember-data';

export default DS.RESTSerializer.extend({

  normalizeArrayResponse(store, primaryModelClass, payload) {

    payload.data = [];
    payload.listing_item._data.forEach(this.formatListingItemArray, payload.data);
    delete payload.listing_item;

    return payload;
  },

  formatListingItemArray(listingItem) {
    this.push({
      type: "listing-item",
      id: listingItem.id,
      attributes: {
        title: listingItem.title,
        description: listingItem.description,
        buy_now_price: listingItem.buy_now_price,
        created_at: listingItem.created_at,
        category_id: listingItem.category_id,
        subcategory_id: listingItem.subcategory_id,
        identity_id: listingItem.identity_id,
        listing_number: listingItem.listing_number,
        brand_new: listingItem.brand_new,
        sold: listingItem.sold,
      },
    });
  },

});

So I suppose my question is, what is Ember Data doing with my data object for this error to occur, and what might I be doing wrong in formatting my data for Ember data to consume.

Thanks in advance, let me know if there's any more information I need to provide in order to give better context.




Aucun commentaire:

Enregistrer un commentaire