javascript - 将 JSON 从 Contentful 获取到 Mustache

标签 javascript jquery json api contentful

我正在尝试从 Contentful (JSON) 获取我的条目并将它们放入 Mustache 模板中。

以下代码有效,但不适用于模板:

client.getEntries()
.then((response) => {
    $('.result').html(response.items.map((item) => ' Items: ' + item.sys.id).join('\n'));

})
.catch((error) => {
    console.log('\x1b[31merror occured')
    console.log(error)
})

当我尝试相同的过程但将其放入模板中时它不起作用:

client.getEntries()
  .then((response) => {

    var template = $('#myEntries').html();
    var html = Mustache.to_html(template, response.data);
    $('.result').html(html);

  })
  .catch((error) => {
    console.log('Error occured')
    console.log(error)
  })

HTML

<script id="myEntries" type="text/template">
    {{#items}} {{fields.customerName}} {{/items}}
</script>

<div class="result"></div>

JSON 字符串似乎没有正确附加到模板?

JSON 文件如下所示

  {
     "sys":{
        "type":"Array"
     },
     "total":3,
     "skip":0,
     "limit":100,
     "items":[
        {
           "sys":{
              "space":{
                 "sys":{
                    "type":"Link",
                    "linkType":"Space",
                    "id":"4eewqivb4aog"
                 }
              },
              "id":"4moramDebKGsUwI2a6YOSe",
              "type":"Entry",
              "createdAt":"2017-03-31T17:31:25.994Z",
              "updatedAt":"2017-03-31T17:31:25.994Z",
              "revision":1,
              "contentType":{
                 "sys":{
                    "type":"Link",
                    "linkType":"ContentType",
                    "id":"customer"
                 }
              },
              "locale":"sv-SE"
           },
           "fields":{
              "customerName":"3",
              "customerUrl":"3"
           }
        },
        {
           "sys":{
              "space":{
                 "sys":{
                    "type":"Link",
                    "linkType":"Space",
                    "id":"4eewqivb4aog"
                 }
              },
              "id":"5M6NPWMve0YgMcSUcoAusk",
              "type":"Entry",
              "createdAt":"2017-03-31T17:31:51.535Z",
              "updatedAt":"2017-03-31T17:31:51.535Z",
              "revision":1,
              "contentType":{
                 "sys":{
                    "type":"Link",
                    "linkType":"ContentType",
                    "id":"customer"
                 }
              },
              "locale":"sv-SE"
           },
           "fields":{
              "customerName":"4",
              "customerUrl":"4"
           }
        },
        {
           "sys":{
              "space":{
                 "sys":{
                    "type":"Link",
                    "linkType":"Space",
                    "id":"4eewqivb4aog"
                 }
              },
              "id":"2ClsG24K5S6qiAYGi8gEQI",
              "type":"Entry",
              "createdAt":"2017-03-31T17:22:16.490Z",
              "updatedAt":"2017-03-31T17:22:16.490Z",
              "revision":1,
              "contentType":{
                 "sys":{
                    "type":"Link",
                    "linkType":"ContentType",
                    "id":"customer"
                 }
              },
              "locale":"sv-SE"
           },
           "fields":{
              "customerName":"5",
              "customerUrl":"5"
           }
        }
     ]
  }

如果能得到一些帮助那就太好了:)

/奥斯卡

最佳答案

从您显示的代码来看,您似乎将错误的数据传递给模板,响应对象没有任何 data 属性。调用 .toPlainObject() 是一个很好的做法,这样所有附加的帮助器方法都将被删除,最终您将只得到原始数据

你的代码应该是

client.getEntries()
  .then((response) => {

    var template = $('#myEntries').html();
    var html = Mustache.to_html(template, response.toPlainObject());
    $('.result').html(html);

  })
  .catch((error) => {
    console.log('Error occured')
    console.log(error)
  })

关于javascript - 将 JSON 从 Contentful 获取到 Mustache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43155694/

相关文章:

javascript - 从 Javascript Socket (Ps) 上传文件到 Golang API (Gin Gonic)

javascript - 配置/更改 JQuery AJAX 编码

javascript - 如何使用 jQuery 多次打印 HTML 输入字段的内容?

javascript - 根据两个条件进行过滤

json - 如何在 Perl 中将简单的哈希转换为 json?

javascript - 在第一行的第二张图片加载完成后开始加载第二行

javascript - 如何动态添加到 JavaScript 对象?

javascript - 单击单选按钮更改 div 文本

c# - 将 Json 字符串转换为 C# 对象列表

json - 这是什么类型? Swift 中有 NSList 选项吗?