所以在我的代码中,我试图获取一位作者及其相关书籍
这是我的服务器响应
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"}}
]
这是我的序列化器
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);
}
});
所以在我的代码中,我使用usinf queryRecord来获取作者信息
所以在我的Mirage / config.js中,我已经尝试过了
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 };
});
但是我只得到作者的详细信息,而我没有得到书的详细信息
如果我在代码中尝试使用this.get('author.books.length')之类的东西,则会得到0, 我不确定要获取hasMany部分还需要做些什么
任何人都可以告诉我如何获得很多部分
谢谢