mercredi 1 mai 2019

unable to get included(has_many) part in ember-cli-mirage

So in my code i am trying to fetch a author and its related books

this is my server response

data:{
attributes:{name: "joseph"}
type:"Author"
id:"1"
relationships: {books: {data: [{id: "1", type: "books"}, {id: "2", type: "books"}]}}
}
included:
[
{id: "1", type: "books", attributes: {title: "catch22"}},
{id: "2", type: "books", attributes: {title: "catch23"}}
]


and this is my serializer

import { JSONAPISerializer } from 'ember-cli-mirage';

import { underscore } from '@ember/string';



export default JSONAPISerializer.extend({


 alwaysIncludeLinkageData: true,

 keyForAttribute: function(attr) {

   return underscore(attr);

 },

 keyForRelationship: function(rawKey) {
   return underscore(rawKey);

 }

});

so in my code i m usinf queryRecord to fetch the author infomation

so in my mirage/config.js i have tried this

this.get('/authors', () => {
    some logic i need to implement here 
    var author = server.db.authors.find(1)
    // debugger
    var included = []
    var book_array = []
    var included_array = []
    if(author.bookIds != null){
      author.bookIds.forEach((book) => {
        var book = server.db.books.find(book)
        book_array.push({id: book.id, type: "books"})
        included_array.push({id: book.id, type: "books", attributes: book})

      })
      included =  included_array
    }

    return {data: {type: 'authors', id: author.id, attributes: author, relationships: {books:book}}, included:  included_array };
});


but i am only getting the author details, and i m not getting the book details

if i try something like this.get('author.books.length') in my code, i get 0, i am not sure what else needs to be done to get the hasMany part

Can any one tell me how to get the has many part

Thanks




Aucun commentaire:

Enregistrer un commentaire