That works, but it's messy and hard to follow.. this is where relate helps you out. Notice getRelationship deals with singular, or plural relationships just fine.
var relate =require("jsonapi-relate")
MyAPI.get("/articles").then(function(payload){
returnpayload.data.map(function(article){
var author =relate.getRelationship(payload, article,"author")
var author =relate.getRelationship(payload, comment,"author")
return{
body:comment.attributes.body,
author: author &&author.attributes
}
})
}
})
})
Get deep Relationships
Using our first example. Let's say that I just want to get Comment authors, and don't care about the comments themselves. Now, I'll need to traverse through two relationships.
var relate =require("jsonapi-relate")
MyAPI.get("/articles").then(function(payload){
returnpayload.data.map(function(article){
var author =relate.getRelationship(payload, article,"author")