I am working on making a blog using WordPress as a headless CMS and Express Handlebars for the views.
In the controller I have:
var axios = require('axios');
/* GET home page data */
exports.getHomepageData = async (req, res, next) => {
try {
let [postCaregoriesData, postData] = await Promise.all([
axios.get(`${base_url}/categories`),
axios.get(`${base_url}/posts?_embed`),
]);
const posts = postData.data;
const categories = postCaregoriesData.data;
posts.forEach(function(post){
console.log(post._embedded.author[0].name);
});
res.render('index',
{
layout: 'layout',
pageTitle: 'All posts',
categories: categories,
posts: posts,
});
} catch (error) {
res.status(500).json(error);
}
};
This line displays the autor's name in the terminal:
posts.forEach(function(post){
console.log(post._embedded.author[0].name);
});
In the view however, where I sucesfully display the post title, I use this to dysplay the post author name:
{{#each posts}}
{{this._embedded.author[0].name}}
{{/each}}
This fails with the error:
Expecting 'ID', got 'INVALID'