Strapi 4.6后端,Nextjs 13作为前端

toiithl6  于 2023-02-22  发布在  其他
关注(0)|答案(2)|浏览(126)

我怎样才能得到存储在Strapi 4.6中的图像显示在屏幕上与nextjs 13:

export default function Page({ params }) {
    let movie = use(getMovie(params.movieID));
    console.log("Movie This is the text", movie);
    //console.log(params.movieID);
    //console.log("Database output !!!", movie.${params.movieID}.?populate=*);
    //const API_URL = ('http://127.0.0.1:1337');

    return (
      <div>
        <p className="text-3xl">{movie.data.attributes.title}</p>
        <p className="mt-2">Year: {movie.data.attributes.year}</p>
        <p className="mt-2">Description: {movie.data.attributes.description}</p>
        <Image className='mt-2'
          src={movie.data.attributes.image.data.attributes.url}
          alt={movie.data.attributes.title} 
          width={500} 
          height={349}
        />
      </div>
    );
}

电影的标题、年份和描述都可以固定在屏幕上。
在服务器端,我收到以下错误:

event - compiled client and server successfully in 145 ms (545 modules)
The requested resource isn't a valid image for /uploads/2008_Jaden_3c49c0fe45.jpg received text/html; charset=utf-8

我的next.config.js文件的设置是:

/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    appDir: true,
  },
  images: {
    remotePatterns: [
      {
        protocol: "http",
        hostname: "127.0.0.1",
        port: "1337",
        pathname: "/**",
      },
    ],
  },
};

module.exports = nextConfig;

Strapi 4.6返回以下API:

// 20230217152302
// http://127.0.0.1:1337/api/movies?populate=*

{
  "data": [
    {
      "id": 1,
      "attributes": {
        "createdAt": "2023-02-03T15:39:55.796Z",
        "updatedAt": "2023-02-07T11:39:47.652Z",
        "publishedAt": "2023-02-03T15:40:08.569Z",
        "title": "Lord of the Rings",
        "year": 2001,
        "description": "A meek Hobbit from the Shire and eight companions set out on a journey to destroy the powerful One Ring and save Middle-earth from the Dark Lord Sauron",
        "slug": "lord-of-the-rings",
        "image": {
          "data": {
            "id": 3,
            "attributes": {
              "name": "6FDUBGB.jpg",
              "alternativeText": null,
              "caption": null,
              "width": 2000,
              "height": 2496,
              "formats": {
                "thumbnail": {
                  "name": "thumbnail_6FDUBGB.jpg",
                  "hash": "thumbnail_6_FDUBGB_f8b45dbbae",
                  "ext": ".jpg",
                  "mime": "image/jpeg",
                  "path": null,
                  "width": 125,
                  "height": 156,
                  "size": 8.49,
                  "url": "/uploads/thumbnail_6_FDUBGB_f8b45dbbae.jpg"
                },
                "small": {
                  "name": "small_6FDUBGB.jpg",
                  "hash": "small_6_FDUBGB_f8b45dbbae",
                  "ext": ".jpg",
                  "mime": "image/jpeg",
                  "path": null,
                  "width": 401,
                  "height": 500,
                  "size": 62.75,
                  "url": "/uploads/small_6_FDUBGB_f8b45dbbae.jpg"
                },
                "medium": {
                  "name": "medium_6FDUBGB.jpg",
                  "hash": "medium_6_FDUBGB_f8b45dbbae",
                  "ext": ".jpg",
                  "mime": "image/jpeg",
                  "path": null,
                  "width": 601,
                  "height": 750,
                  "size": 117.55,
                  "url": "/uploads/medium_6_FDUBGB_f8b45dbbae.jpg"
                },
                "large": {
                  "name": "large_6FDUBGB.jpg",
                  "hash": "large_6_FDUBGB_f8b45dbbae",
                  "ext": ".jpg",
                  "mime": "image/jpeg",
                  "path": null,
                  "width": 801,
                  "height": 1000,
                  "size": 182.81,
                  "url": "/uploads/large_6_FDUBGB_f8b45dbbae.jpg"
                }
              },
              "hash": "6_FDUBGB_f8b45dbbae",
              "ext": ".jpg",
              "mime": "image/jpeg",
              "size": 597.08,
              "url": "/uploads/6_FDUBGB_f8b45dbbae.jpg",
              "previewUrl": null,
              "provider": "local",
              "provider_metadata": null,
              "createdAt": "2023-02-07T11:39:40.120Z",
              "updatedAt": "2023-02-07T11:39:40.120Z"
            }
          }
        }
      }
    },
    {
      "id": 2,
      "attributes": {
        "createdAt": "2023-02-03T15:42:51.349Z",
        "updatedAt": "2023-02-07T11:40:12.979Z",
        "publishedAt": "2023-02-03T15:42:52.112Z",
        "title": "Star Wars",
        "year": 1977,
        "description": "Luke Skywalker joins forces with a Jedi Knight, a cocky pilot, a Wookiee and two droide to save the galaxy from the Empire's wold-destroying battle station, while also attemting te rescue Princess Leia from the mysterious Darth Vader.",
        "slug": "star-wars",
        "image": {
          "data": {
            "id": 4,
            "attributes": {
              "name": "2008 Jaden.jpg",
              "alternativeText": null,
              "caption": null,
              "width": 1868,
              "height": 1304,
              "formats": {
                "thumbnail": {
                  "name": "thumbnail_2008 Jaden.jpg",
                  "hash": "thumbnail_2008_Jaden_3c49c0fe45",
                  "ext": ".jpg",
                  "mime": "image/jpeg",
                  "path": null,
                  "width": 223,
                  "height": 156,
                  "size": 9.92,
                  "url": "/uploads/thumbnail_2008_Jaden_3c49c0fe45.jpg"
                },
                "small": {
                  "name": "small_2008 Jaden.jpg",
                  "hash": "small_2008_Jaden_3c49c0fe45",
                  "ext": ".jpg",
                  "mime": "image/jpeg",
                  "path": null,
                  "width": 500,
                  "height": 349,
                  "size": 35.21,
                  "url": "/uploads/small_2008_Jaden_3c49c0fe45.jpg"
                },
                "medium": {
                  "name": "medium_2008 Jaden.jpg",
                  "hash": "medium_2008_Jaden_3c49c0fe45",
                  "ext": ".jpg",
                  "mime": "image/jpeg",
                  "path": null,
                  "width": 750,
                  "height": 524,
                  "size": 65.83,
                  "url": "/uploads/medium_2008_Jaden_3c49c0fe45.jpg"
                },
                "large": {
                  "name": "large_2008 Jaden.jpg",
                  "hash": "large_2008_Jaden_3c49c0fe45",
                  "ext": ".jpg",
                  "mime": "image/jpeg",
                  "path": null,
                  "width": 1000,
                  "height": 698,
                  "size": 102.08,
                  "url": "/uploads/large_2008_Jaden_3c49c0fe45.jpg"
                }
              },
              "hash": "2008_Jaden_3c49c0fe45",
              "ext": ".jpg",
              "mime": "image/jpeg",
              "size": 258.7,
              "url": "/uploads/2008_Jaden_3c49c0fe45.jpg",
              "previewUrl": null,
              "provider": "local",
              "provider_metadata": null,
              "createdAt": "2023-02-07T11:40:06.583Z",
              "updatedAt": "2023-02-07T11:40:06.583Z"
            }
          }
        }
      }
    }
  ],
  "meta": {
    "pagination": {
      "page": 1,
      "pageSize": 25,
      "pageCount": 1,
      "total": 2
    }
  }
}

希望有人能帮我一把?谢谢你的帮助!
罗伯特

jjjwad0x

jjjwad0x1#

Strapi只返回你的图片(或媒体)的相对路径,你需要在这些路径前面加上你的Strapi所在的URL/域。
例如:

  • "示例网"+"/上传文件/2008年翡翠http://127.0.0.1:1337" + "/uploads/2008_Jaden_3c49c0fe45.jpg"
  • "示例网"+"/上传文件/2008年翡翠https://my-hosted-strapi.com" + "/uploads/2008_Jaden_3c49c0fe45.jpg"
64jmpszr

64jmpszr2#

多亏了khangnd的帮助才能让它工作起来。

export default function Page({ params }) {
    let movie = use(getMovie(params.movieID));
    const API_URL = ('http://127.0.0.1:1337');

    return (
      <div>
        <p className="text-3xl">{movie.data.attributes.title}</p>
        <p className="mt-2">Year: {movie.data.attributes.year}</p>
        <p className="mt-2">Description: {movie.data.attributes.description}</p>
        <Image className='mt-2'
          src={`${API_URL}${movie.data.attributes.image.data.attributes.url}`}
          alt={movie.data.attributes.title} 
          width={500} 
          height={349}
        />
      </div>
    );
}

相关问题