【问题标题】:Sanity Error: `dataset` must be provided to perform queries with React理智错误:必须提供“数据集”才能使用 React 执行查询
【发布时间】:2021-08-17 01:59:56
【问题描述】:

我正在尝试使用 Sanity CMS 创建博客;当我尝试从 Sanity 数据库中获取数据时,我明白了

“错误:必须提供dataset 才能执行查询”

我得到了带有 projectID 和数据库名称的 client.js 组件

给我错误的组件如下所示:

import React, { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import sanityClient from "../client.js";
export default function AllPosts() {
  const [allPostsData, setAllPosts] = useState(null);
  useEffect(() => {
    sanityClient
      .fetch(
        `*[_type == "post"]{
        title,
        slug,
        mainImage{
          asset->{
          _id,
          url
        }
      }
    }`
      )
      .then((data) => setAllPosts(data))
      .catch(console.error);
  }, []);

  return (
    <div>
      <h2>Blog Posts</h2>
      <h3>Welcome to my blog posts page!</h3>
      <div>
        {allPostsData &&
          allPostsData.map((post, index) => (
            <Link to={"/" + post.slug.current} key={post.slug.current}>
              <span key={index}>
                <img src={post.mainImage.asset.url} alt="" />
                <span>
                  <h2>{post.title}</h2>
                </span>
              </span>
            </Link>
          ))}
      </div>
    </div>
  );
}

【问题讨论】:

    标签: javascript reactjs dataset sanity


    【解决方案1】:

    使用相同的教程,并遇到相同的错误。在“client.js”下,确保数据库设置为“production”。

    【讨论】:

      猜你喜欢
      • 2021-11-27
      • 2018-12-15
      • 1970-01-01
      • 2020-04-23
      • 2017-07-28
      • 2020-11-30
      • 2019-06-09
      • 2021-07-12
      • 1970-01-01
      相关资源
      最近更新 更多