首页 > 解决方案 > 内容丰富的 getAsset 返回 undefined

问题描述

我正在尝试从 Contentful 获取资产、图像到我的 Angular 应用程序中。在网络日志中我得到图像时,它一直undefined在我的 console.log 中显示。知道为什么吗?

这是组件中的功能:

    this.contentfulService.getContactImage()
      .then(asset => this.pageImage)
      .then(asset => console.log("Recieved Gallery Image", this.pageImage));

这是服务:

  //get Page Image
  getContactImage() {
    return this.cdaClient.getAsset('<asset ID>')
      .then(asset => asset.fields.file.url);
  }

标签: javascriptangulartypescriptcontentful

解决方案


你不认为,你正在返回this.pageImage你从未分配过的东西。

this.contentfulService.getContactImage()
      .then(asset => **this.pageImage**)
      .then(asset => console.log("Recieved Gallery Image", this.pageImage));

推荐阅读