Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. /** Route **/
  2. router.get("/",(req,res,next) => {
  3.  
  4.  
  5. var posts = models.Post.findAll({
  6. attributes: ["id","title"],
  7. include: [{
  8. model: models.Category,
  9. as: "Categories",
  10. attributes: ["id","title"]
  11. }]
  12. })
  13. .then( posts => {
  14. res.json(posts);
  15. })
  16. .catch(function(err){
  17. console.log(err);
  18. res.status(400).send({error: "Bad Request - Malformed request."});
  19. });
  20.  
  21.  
  22. });
  23.  
  24.  
  25.  
  26. /** Result output
  27. [
  28. {
  29. "id": 1,
  30. "title": "Test Post",
  31. "Categories": [
  32. {
  33. "id": 1,
  34. "title": "Test Category",
  35. "PostsCategories": {
  36. "post_id": 1,
  37. "category_id": 1
  38. }
  39. }
  40. ]
  41. }
  42. ]
  43.  
  44. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement