vendredi 11 mai 2018

Relationship with 2 API's getting error

Please help me, it's for a job interview!!

I have 2 API's:

  1. http://example.api.com/api.json (this file have aprox 5mb)
  2. http://api.com/:itemId

The 1st one have all data that i need, like this:

{
"realms": [
{"name":"Azralon","slug":"azralon"}],
"auctions": [
{"auc":828911977,"item":76139,"owner":"Bloodkina","bid":15294990,"buyout":16099990,"quantity":10,"timeLeft":"VERY_LONG"},
{"auc":828911979,"item":10000,"owner":"Bloodkina", "bid":15294990,"buyout":16099990,"quantity":100,"timeLeft":"VERY_LONG"},
{"auc":829305192,"item":98828,"owner":"Tempestivå","bid":15294990,"buyout":16099990,"quantity":5,"timeLeft":"VERY_LONG"},
{"auc":829305193,"item":98728,"owner":"Tempestivå", "bid":15294990,"buyout":16099990,"quantity":2,"timeLeft":"VERY_LONG"}
]}

The 2nd one have the name of the Items, but it only responds when i pass itemId as parameter. For example the item:76139, like http://api.com/76139

{
    "id": 76139,
    "description": "",
    "name": "Wild Jade",
    "icon": "inv_misc_gem_x4_rare_uncut_green",
}

I want to show the name of item and owner, but i getting an error like <DS.PromiseObject:ember71726> im my item field, the owner field is ok. How can i do this?? (it's the Blizzard API for auctions and items)

model/auction.js

import DS from 'ember-data';

export default DS.Model.extend({
  auc: DS.attr('number'),
  items: DS.belongsTo('item'),
  owner: DS.attr('string'),
});

model/item.js

import DS from 'ember-data';

export default DS.Model.extend({
  auctions: DS.hasMany('auction'),
  name: DS.attr('string')
});

routes/index.js

import Route from '@ember/routing/route';

export default Route.extend({
  model(){
    return this.store.findAll('auction');
  }
});

routes/item.js

import Route from '@ember/routing/route';

export default Route.extend({
  model(params){
    return this.store.findRecord('item', params.item_id)
  },
});

serializers/auction.js

import DS from 'ember-data';

export default DS.RESTSerializer.extend({
  normalizeResponse (store, primaryModelClass, payload, id, requestType) {
    return {
      realms: payload.realms,
      data: payload.auctions.map(ah=>{
        return {
        id: ah.auc,
        type:'auction',
        attributes: ah
        }
      })
    };
  }
});

serializers/item.js

import DS from 'ember-data';

export default DS.JSONSerializer.extend({
  normalizeResponse (store, primaryModelClass, payload, id, requestType) {
    payload = {
      data : payload,
      id: payload.id,
      name: payload.name
    };
    return this._super(store, primaryModelClass, payload, id, requestType)
  }
});

templates/index.hbs


<ul>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
</ul>


Error




Aucun commentaire:

Enregistrer un commentaire