0

我得到 include is not a function 错误,并且已经 record.id 无法识别 VS Code 上的 .includes()

我不知道问题出在哪里?

在此处输入图像描述

import React, { Component } from 'react'
import axios from "axios"

export default class Tablo extends Component {
     constructor(props){
    super(props)
    this.state={
      records:[],
      id:1
    }
}

render() {
    return (
        <div>
        {
            this.state.records.filter(record => record.id.includes(this.state.id))
            .map(rec => (
                <li> {rec.shop}</li>
            ))
        }

        </div>
    )
}
componentDidMount() { 
    axios.get('http://localhost:8080/kayitlar').then(res => {
      console.log(res);
      this.setState({records:res.data});
    });
}
4

2 回答 2

0

includes() 方法确定字符串是否包含指定字符串的字符。

我认为您正在申请号码,所以给出错误。

于 2020-01-30T12:12:21.457 回答
0

看,包含仅用于字符串数组或字符串比较,如果字符串包含另一个字符串的一部分。

基于此,您有两种选择来解决您的问题

  1. 检查是否record.id已设置并且其类型是字符串或数组
  2. 假设 id 应该是唯一的,比较它们的最佳方法是使用==operator
于 2020-01-30T12:12:43.383 回答