Answer the question
In order to leave comments, you need to log in
Property 'title' does not exist on type, Argument of type?
i wrote code that can get 2 types of array from server but i get error 23.25 lines I get two Errors The 'title' property does not exist for the type and the type argument faceProductList[][] code
interface faceProductList {
readonly title: string;
readonly price: string;
readonly prodState: string;
readonly shipping: string;
readonly sold: string;
readonly src: string;
readonly id: string;
readonly to: string;
}
class Server {
private url: string = 'https://foo0022.firebaseio.com/';
public async request(id: string): Promise<(faceProductList[] | faceProductList)[]> {
const res = await fetch(`${this.url}${id}`);
const resArr: (faceProductList[] | faceProductList)[] = await res.json();
return resArr;
}
public async handler(id: string, valueSearch: string) {
await this.request(id)
.then((array) => {
if(id){
return array.filter(({title}) => title.includes(valueSearch))
}else{
return [].concat(...array)
}
})
}
}
Answer the question
In order to leave comments, you need to log in
Because this.request(id) returns either faceProductList[][] or faceProductList[].
And array.filter(({title}) => title.includes(valueSearch)) uniquely treats the data as faceProductList[] and doesn't take into account faceProductList[][].
Can do
public async handler(id: string, valueSearch: string) {
await this.request(id)
.then((array: faceProductList[]) => {
...
public async request(id: string): Promise<faceProductList[][]> {
const res = await fetch(`${this.url}${id}`);
const resArr: faceProductList[] | faceProductList[][] = await res.json();
if (resArr.length === 0) {
return resArr;
}
if (Array.isArray(resArr[0])) {
return resArr;
}
return [resArr];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question