How it works...

The format for our JSON API requests from http://localhost:3000/api/posts will now contain a fully embedded user object, but with an ID that matches an independent user record:

"data": [
{
"type": "posts",
"id": "596aafb6aede86ed94d5261d",
"attributes": {
"title": "dolores earum et",
"content": "Tempora qui est sed minima cupiditate...",
"published": "2016-08-09T13:56:03.378Z",
"html": "<p>Tempora qui est sed minima cupiditate...."
},
"relationships": {
"author": {
"data": {
"type": "users",
"id": "596aafb6aede86ed94d5261c"
}
}
}
},
...
"included": [
{
"type": "users",
"id": "596aafb6aede86ed94d5261c",
"attributes": {
"first-name": "Merl",
"last-name": "Bahringer",
"email": "[email protected]"
}
},
...

The JSON API response for our http://localhost:3000/api/users endpoint will include our user details, but will not include anything more than a list of post document objectId references:

{
"data": {
"type": "users",
"id": "596aafb6aede86ed94d5261c",
"attributes": {
"first-name": "Merl",
"last-name": "Bahringer",
"email": "[email protected]",
"role": "author"
},
"relationships": {
"posts": {
"data": [
{
"type": "posts",
"id": "596aafb6aede86ed94d5261d"
},
{
"type": "posts",
"id": "596aafb6aede86ed94d5261e"
},
{
"type": "posts",
"id": "596aafb6aede86ed94d5261f"
}
]
}
}
}
}

We can actually access the properties of the referenced post documents using another operation called populate. In the next recipe, we'll learn how to use populate with these sorts of object references in Mongoose.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.77.117