简体   繁体   English

函数返回数据以过滤和映射显示错误 React.js TypeError:无法读取未定义的属性“val”

[英]function returning the data to filter and map that show error React.js TypeError: Cannot read property 'val' of undefined

I'm try to collect some collection of data from Firebase.我正在尝试从 Firebase 收集一些数据集合。 Filter map to fronted using React Js.使用 React Js 过滤映射到前端。 In below code Firebase.database().ref('public/post/India/Tamil Nadu/SALEM/').on('value', this.getData, this.errordata) Using this function i get data on this.getdata()在下面的代码Firebase.database()。REF( '公共/后/印度/泰米尔纳德邦/塞勒姆/')。在( '值',this.getData,this.errordata)使用此功能我得到this.getdata数据()

I got data that show in console output but browser show error as TypeError: Cannot read property 'val' of undefined.我得到的数据显示在控制台输出中,但浏览器将错误显示为TypeError:无法读取未定义的属性“val”。 Please any one solve this请任何人解决这个问题

Please Note请注意

  • I successfully retrieve my data From firebase That show in console output.我成功地从控制台输出中显示的 firebase 检索了我的数据。
  • The issue while getdata function returning the data to filter and map that show error getdata 函数返回数据以过滤和映射显示错误的问题

-> Home.js -> 主页.js

 import React, { Component } from 'react'; import Firebase from './container/Firebase'; class home extends Component { constructor(props) { super(props); this.state = { nam: "a", arr:"", users: ['abc', "pdsa", "eccs", "koi"] } } componentWillMount() { Firebase.database().ref('public/post/India/Tamil Nadu/SALEM/').on('value', this.getData, this.errordata) } getData(data) { console.log("sdata"); //console.log(data.val()); var post = data.val(); console.log(post); var keyy = Object.keys(post); console.log(keyy); var lisarr =[''] ; for (var i = 0; i < keyy.length; i++) { var k = keyy[i]; lisarr[i]={ key :k , Organisation : post[k].Organisation, Role :post[k].Role } } console.log("getData lisarr"); console.log(lisarr); return lisarr; } errordata(err) { console.log(err); } handleChange = event => { this.setState({ nam: event.target.value }); }; render() { return ( <div> <h1>Homa</h1> {this.getData().filter(person => this.state.num === null ||person.Role.includes("h")).map(filteredPerson => ( <li> {filteredPerson.Role} </li> ))} </div> ) } } export default home;

Console output :控制台输出:

 getData post                                                                     home.js:24
 {-MFJqzPGXeLPk6pQzY18: {…}, -MFKZLLtWkH2arNnmNE_: {…}, -MFKZg38ybFO9hC0UqLf: {…}}- 
       -MFJqzPGXeLPk6pQzY18: {City: "SALEM", Country: "India", Description: "Proven experienc...
       -MFKZLLtWkH2arNnmNE_: {City: "SALEM", Country: "India", Description: "attend customers↵...
       -MFKZg38ybFO9hC0UqLf: {City: "SALEM", Country: "India", Description: "attend customers↵...

                                                                                     home.js:26 
 getData keyy                                                                        home.js:27 
 ["-MFJqzPGXeLPk6pQzY18", "-MFKZLLtWkH2arNnmNE_", "-MFKZg38ybFO9hC0UqLf"]
           0: "-MFJqzPGXeLPk6pQzY18"
           1: "-MFKZLLtWkH2arNnmNE_"
           2: "-MFKZg38ybFO9hC0UqLf"

  getData  lisarr                                                                  home.js:45 

 (3) [{…}, {…}, {…}]
  0:
      Organisation: " abc CAR COMPANY PVT LTD "
      Role: "Sales Executive"
      key: "-MFJqzPGXeLPk6pQzY18"
  1:
      Organisation: "abc conseltency"
      Role: "Surgical Avenue"
      key: "-MFKZLLtWkH2arNnmNE_"

  2:
       Organisation: "Sociall.in "
       Role: "Excutive"
       key: "-MFKZg38ybFO9hC0UqLf"
  

My browser show error as我的浏览器显示错误为

  TypeError: Cannot read property 'val' of undefined
  19 | console.log("sdata");
  20 | //console.log(data.val());
  21 | 
> 22 | var post = data.val();
     | ^  23 | console.log(post);
  24 | var keyy = Object.keys(post);
  25 | console.log(keyy);

I think you are not passing snapshot to the function that is why it is showing undefined.我认为您没有将快照传递给函数,这就是它显示未定义的原因。 Try this out:试试这个:

   ...
    this.state = {
    fetchedData: null,
            nam: "a",
            ...
        }
   ...
    componentDidMount() {

       Firebase.database().ref('public/post/India/Tamil Nadu/SALEM/')
        .on('value', (snapshot) => { 
            if(snapshot.val() != null){
             this.setState({fetchedData: snapshot.val());
            }else {
             this.errordata();
        }});
       
    }
...

{this.getData(this.state.fetchedData).filter(person => this.state.num === null ||person.Role.includes("h")).map(...)

Please refer: https://firebase.google.com/docs/database/web/read-and-write#listen_for_value_events请参考: https : //firebase.google.com/docs/database/web/read-and-write#listen_for_value_events

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React.js 错误:TypeError:无法读取未定义的属性“地图” - React.js error :TypeError: Cannot read property 'map' of undefined TypeError:无法读取 react.js 中未定义的属性“地图” - TypeError: Cannot read property 'map' of undefined in react.js TypeError:无法读取未定义React.js的属性“ map” - TypeError: Cannot read property 'map' of undefined React.js TypeError:无法读取React.js上未定义的属性&#39;map&#39; - TypeError: Cannot read property 'map' of undefined on React.js 错误:无法读取未定义 React.JS 的属性“map” - Error : Cannot read property 'map' of undefined React.JS TypeError:无法读取 Netlify CMS/React.js 上未定义的属性“地图” - TypeError: Cannot read property 'map' of undefined on Netlify CMS/React.js TypeError:无法读取 react.js 中未定义的属性“长度” - TypeError: Cannot read property 'length' of undefined in react.js 未捕获的类型错误:无法读取未定义的属性“状态”-React.js - Uncaught TypeError: Cannot read property 'state' of undefined - React.js 类型错误:无法读取未定义的属性“查询”(react.js) - TypeError: Cannot read property 'queries' of undefined (react.js) TypeError:无法读取未定义React.js的属性“字段” - TypeError: Cannot read property 'fields' of undefined React.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM