[英]Property 'products' does not exist on type 'Readonly<{}>'
EDIT on @Lesiak request:编辑@Lesiak 请求:
Here is my getProducts call这是我的 getProducts 电话
// @api/shopifyProducts.ts
import Client from 'shopify-buy'
const client = Client.buildClient({
// TODO: add to dotenv
domain: 'some-domain.myshopify.com',
storefrontAccessToken: 'example-token-2597293846729587293875'
})
export const getProducts = async () => {
try {
const data = await client.product.fetchAll()
const products = await data.map((item) => {
return {
title: item.title,
description: item.description
// images: item.images
}
})
return products
} catch (error) {
throw new Error(`Product API fetch failed: ${error}`)
}
}
I have also refactored my component like this:我还像这样重构了我的组件:
import React, { Component } from 'react'
import { getProducts } from '@api/shopifyProducts'
class TheListProducts extends Component<{}> {
constructor(props) {
super(props)
this.state = {
products: null
}
}
async componentDidMount() {
this.setState({
products: await getProducts()
})
console.log(this.state.products) ==> Error: Property 'products' does not exist on type 'Readonly<{}>'
}
render() {
return <p>Hey</p>
}
}
export default TheListProducts
Morning fellow developers,各位开发者早安,
I stumbled upon an issue I can't solve on my own even by doing an extensive research on the web.我偶然发现了一个即使对 web 进行广泛研究也无法自行解决的问题。 Since I am new to TS, I can see I don't understand in 100% what is really happening.
由于我是 TS 的新手,我可以看到我 100% 不明白到底发生了什么。
I have a component in React, where I async fetch content from Shopify content and I want to print it inside render function.我在 React 中有一个组件,我从 Shopify 内容中异步获取内容,我想在渲染 function 中打印它。
import React, { Component } from 'react'
import { getProducts } from '@api/shopifyProducts'
interface Product {
title: any
description: any
}
interface ListState {
products: {
[key: string]: Product | Function
}
}
class TheListProducts extends Component<{}, ListState> {
async componentDidMount() {
this.setState({
products: await getProducts()
})
}
render() {
return <p>{this.state.products}</p>
}
}
export default TheListProducts
I receive the following error:我收到以下错误:
(property) products: {
title: string;
description: string;
}[]
Type '{ title: string; description: string; }[]' is not assignable to type '{ [key: string]: Product; }'.
Index signature is missing in type '{ title: string; description: string; }[]'.ts(2322)
Here is how it looks in the browser:这是它在浏览器中的外观:
I can read, and I think I understand the error, but have no clue whatsoever what to do about it.我可以阅读,我想我理解这个错误,但不知道该怎么做。
If anyone could explain it to me in plain english it would be awesome.如果有人能用简单的英语向我解释,那就太棒了。
Based on the web research I tried enhancing the Product interface like this:基于 web 研究,我尝试像这样增强产品界面:
interface Product {
title: string
description: string
[key: string]: string | number | undefined | Function
}
but it's like going in blind...但这就像盲人一样...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.