类型错误:_client_js__WEBPACK_IMPORTED_MODULE_2___default.a.fetch 不是函数。如何解决这个问题呢?

时间:2021-01-03 06:39:50

标签: reactjs

我在使用 react hooks 时遇到了这个错误。

从 'react' 导入 React, {useState, useEffect}; 从 '../client.js' 导入 sanityClient

 export default function Post() {
 const [postData, setPost] = useState(null);
   7 | 
   8 |    useEffect(() => {
>  9 |        sanityClient.fetch(
     | ^  10 |                  `*[_type == "post"]{
  11 |                  title,
  12 |                  slug,
                    mainImage{
                      asset => {
                          _id,
                          url
                      },
                      alt
                  }
              }`
        )
        .then((data) => setPost(data))
        .catch(console.error);
    }, []);

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,在检查我的代码后发现多个错误 喜欢:

  • 从“@sanity/client”导入 sanityClient(这不是你的情况)

  • mainImage{asset => {_id, url }, should be ``mainImage{asset -> {_id, url }用破折号而不是等号

所以我的建议是仔细检查你的拼写