I am currently developing an application using Express, Postgres as the database, and Sequelize as the Object-Relational Mapping (ORM) tool.
After executing my code, I received the following response:
{
"user": {
"user_name": "John",
"post": [
{
"id": 1,
"created_at": "2018-04-16T22:52:59.054Z",
"post_titles": [
{
"title_id": 3571
},
{
"title_id": 3570
},
{
"title_id": 3569
}
]
}
]
}
}
My question is, how can I determine the total number of titles within a post?
For reference, I have four models:
User
, which has many Posts
Post
, which has many Titles
through PostTitles
Title
PostTitle
Here is the query I used:
User.findOne({
where: { id: req.params.id },
attributes: ['id', 'user_name'],
include: [
{ model: Post,
attributes: ['id', 'created_at'],
include: {
model: PostTitles,
attributes: ['title_id']
}
}
]
})
Any assistance on this matter would be greatly appreciated!