I am attempting to retrieve the most recent blog posts from Hubspot through their API, which requires accessing 2 endpoints (one for the blog posts and one for the tag information associated with each post).
The initial call fetches the latest blog posts. Then, for each post, I need to fetch the tag information related to that post.
Below is the code that implements the process described above:
async getBlogs(contentGroupID: string = null, limit: number = null) {
this.url = 'https://api.hubapi.com/cms/v3/blogs/posts?state=PUBLISHED&sort=-publish_date';
const posts = await useFetch(
this.url
+ '&hapikey=' + this.HS_API_KEY
+ (contentGroupID ? '&content_group_id=' + contentGroupID : null)
+ (limit ? '&limit=' + limit : null)
).then(response => {
return response;
});
const tags = (posts.data.value as any).results.map(post => fetch(`https://api.hubapi.com/blogs/v3/topics/${post.tagIds[0]+'?hapikey='+this.HS_API_KEY}`));
const taggs = await Promise.all(tags);
const payload = new Object();
payload['blogPosts'] = posts;
payload['tags'] = taggs;
return payload;
}
Here is the browser response:
https://i.sstatic.net/IaBba.png
In the screenshot above, the blogPosts
object resolves successfully, but the part with Promise.all()
to retrieve the tagIds[0]
does not. It returns [object Response]
.
I have attempted various methods to resolve this issue, but it always returns the unresolved Promise object. What is the correct approach to handle this and obtain the actual data?
As an example of what the tag
data object should look like, here is the data for the tag with id 41520763199:
{
"categoryId": 3,
"contentIds": [],
"created": 1613125431347,
"deletedAt": 0,
"description": "",
"id": 41520763199,
"language": "en-us",
"name": "Case Studies",
"portalId": 2734675,
"slug": "case-studies",
"translations": {},
"updated": 1616091635999
}