代码之家  ›  专栏  ›  技术社区  ›  Razvan Zamfir

是什么原因导致无法在Handlebars视图中显示来自WordPress API的帖子作者名称?

  •  0
  • Razvan Zamfir  · 技术社区  · 3 年前

    我正在制作一个博客,使用 WordPress作为无头CMS 快速把手 查看。

    在控制器中,我有:

    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);
      }
    };
    

    此行显示终端中的自动驾驶器名称:

     posts.forEach(function(post){
        console.log(post._embedded.author[0].name);
     });
    

    然而,在视图中,如果我成功地显示了文章标题,我会使用它来显示文章作者的姓名:

    {{#each posts}}
       {{this._embedded.author[0].name}}
    {{/each}}
    

    此操作失败,返回错误:

    Expecting 'ID', got 'INVALID'
    

    代码中缺少什么?

    0 回复  |  直到 3 年前