Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript .filter(…)。则不是componentDidMount中的函数_Javascript_Reactjs - Fatal编程技术网

Javascript .filter(…)。则不是componentDidMount中的函数

Javascript .filter(…)。则不是componentDidMount中的函数,javascript,reactjs,Javascript,Reactjs,我试图过滤componentDidMount中的数组,然后在返回项的propertyID上设置状态 componentDidMount() { const pathname = "/path2"; const properties = this.props.properties; properties .edges .filter(node => { return `/${node.slug}` === pathname; }) .t

我试图过滤componentDidMount中的数组,然后在返回项的propertyID上设置状态

componentDidMount() {
  const pathname = "/path2";
  const properties = this.props.properties;
  properties
    .edges
    .filter(node => {
      return `/${node.slug}` === pathname;
    })
    .then(node =>
      this.setState({ propertyID: node.information.propertyID })
    );
}
但是我收到了这个错误

未捕获的TypeError:properties.filter…则不是函数 在ProxyComponent.componentDidMount处

当this.props.properties为


Array.filter不返回承诺。将代码更改为此以使其可运行:

componentDidMount() {
  const pathname = "/path2";
  const properties = this.props.properties;
  const filteredArray = properties
    .edges
    .filter(node => {
      return `/${node.slug}` === pathname;
    });

  filteredArray.forEach((node) => {
    // Computed property, so each node gets set separately in state
    this.setState({ [node.information.propertyID]: node.information.propertyID })
  });
}
试试这个

componentDidMount() {
  const pathname = '/path2';
  const properties = this.props.properties;

  const node = properties.edges.filter(node => {
    return `/${node.slug}` === pathname;
  });

  this.setState({ propertyID: node.information.propertyID });
}

你必须改变过滤线如下,它将工作。在Filter函数中,请注意我是如何使用{}表示法中的节点对象的

在本例中,节点表示整个对象

{
      "node": {
        "id": "121323",
        "slug": "path1",
        "information": {
          "propertyID": "property1"
        }
      }
    }
但您需要这个对象的节点,这就是为什么在filter函数中添加{}

溶液-

变量属性={ 边缘:[ { 节点:{ 身份证号码:121323, 蛞蝓:路径1, 资料:{ propertyID:property1 } } }, { 节点:{ 身份证号码:332342, 蛞蝓:路径2, 资料:{ propertyID:property2 } } }, { 节点:{ 身份证号码:123234, 蛞蝓:路径3, 资料:{ propertyID:property3 } } } ] } console.logproperties .边缘
.filter{node}=>`/${node.slug}`=='/path1'数组筛选器不返回承诺…筛选器不返回承诺,并且不是异步调用。这就是为什么你得不到。然后从中把你的过滤结果保存在一个变量中,然后使用setState。好的,谢谢。我尝试过@NitishNarang作为常量filteredProperty=properties.filternode=>'/${node.slug}'==pathname;但如果它不归还任何东西,我在另一个问题上读到它可以归还一个承诺。根据反对票和你的评论,我认为这是错误的。我希望我在变量中的用法是未定义的,因为我错误地读取了数组。是的,filter总是返回一个新的过滤数组。它没有回报一个承诺。阅读更多内容:……另外,您是否介意说明此问题的预期输出?
{
      "node": {
        "id": "121323",
        "slug": "path1",
        "information": {
          "propertyID": "property1"
        }
      }
    }