简体   繁体   English

地图功能不起作用我做错了什么?

[英]map function wont work what am i doing wrong?

Trying to get the information in this array onto components!试图将这个数组中的信息获取到组件上! Not sure what im doing wrong!!不知道我做错了什么!! Please save me.请救救我。 below is my array of stuffs下面是我的一系列东西

So i get my data from firebase then i do this to it所以我从firebase获取我的数据然后我这样做

function snapshotToArray(snapshot) {
  var returnArr = [];

  snapshot.forEach(function(childSnapshot) {
    var item = childSnapshot.val();
    //item.key = childSnapshot.key;

    returnArr.push(item);
  });

  return returnArr;
};

export function allMyPropertiesDetailsFetch(currentscreen) {
  console.log("Called the property details");
  const { currentUser } = firebase.auth();
  console.log("gotthuser");
  console.log(firebase.database().ref(`/properties/`));
  var propertyref = firebase.database().ref(`/properties/`).orderByChild('userid').equalTo(currentUser.uid);

  propertyref.once('value', snapshot => {
    if (snapshot.exists()) {
      var Property = snapshotToArray(snapshot);
      //     console.log('Property: '+Property);
      currentscreen.setState({
        propertyinfo: Property
      });
    } else {
      //  console.log('There is no user who has email like '+ email)
    }
  })

}

my array looks [{"aptNumber":"","city":"","nickName":"","numberofbathrooms":"2","numberofbedrooms":"1","state":"","streetName":"","streetNumber":"","typeof":"Apartment","userid":"sb7Hj93D08XiaRguQ4cbSjz4vXC3"}] .我的数组看起来 [{"aptNumber":"","city":"","nickName":"","numberofbathrooms":"2","numberofbedrooms":"1","state":""," streetName":"","streetNumber":"","typeof":"Apartment","userid":"sb7Hj93D08XiaRguQ4cbSjz4vXC3"}] 。 STILL doesnt work im calling it like this .仍然不起作用,我这样称呼它。 var persons = JSON.parse(JSON.stringify(this.state.propertyinfo)); var person = JSON.parse(JSON.stringify(this.state.propertyinfo)); console.log(persons);控制台日志(人);

let added_buttons_goes_here = persons.map( (data, index) => { return ( /// psedo code of pass-in remove button as a children this._handleRemoveButton(data.key) } /> ) }); let added_buttons_goes_here = people.map( (data, index) => { return ( /// 传入删除按钮作为子项的psedo代码 this._handleRemoveButton(data.key) } /> ) });

Set your array like this.像这样设置你的数组。

let array = [
 {
   "aptNumber": "",
   "city": "",
   ...   
 },
 {
   "aptNumber": "",
   "city": "",
   ...    
 }
]

console.log(array.map(x => x.key));

The Json you are dealing with seems to be an invalid one.您正在处理的 Json 似乎是无效的。 How do you generate this json object.你如何生成这个json对象。 If you are the one who generate this json in your services, then please try to create a valid json response.如果您是在您的服务中生成此 json 的人,请尝试创建有效的 json 响应。 I have formatted yours as below:我已将您的格式化如下:

[{"aptNumber": "", "city": "", "key": "6bba4c82-18e2-3c10-a798-028f42d97857", "nickName": "My name ", "numberofbathrooms": "2", "numberofbedrooms": "1", "state": "", "streetName": "Fskslal", "streetNumber": "Fjsl", "typeof": "Apartment", "userid": "sb7Hj93D08XiaRguQ4cbSjz4vXC3"}, 
{ "aptNumber": "", "city": "", "key": "714590f6-e8da-2d29-20c2-e5010a3a9df4", "nickName": "BATMAN", "numberofbathrooms": "2", "numberofbedrooms": "0", "state": "", "streetName": "", "streetNumber": "", "typeof": "Apartment", "userid": "sb7Hj93D08XiaRguQ4cbSjz4vXC3"},
{ "aptNumber": "", "city": "", "key": "bd2ad46a-f307-4f8e-1fc8-ba828b6651bc", "nickName": "NOT BATMAN", "numberofbathrooms": "0", "numberofbedrooms": "0", "state": "", "streetName": "", "streetNumber": "", "typeof": "Apartment", "userid": "sb7Hj93D08XiaRguQ4cbSjz4vXC3"} ]

If you consume this json from other parties, then you will have to do some manipulations by keeping this as as String, but its not a clean way of doing/parsing a json response.如果您从其他方使用此 json,那么您将必须通过将其保留为 String 来进行一些操作,但这不是一种执行/解析 json 响应的干净方式。

Figured it out guys想通了伙计们

I was declaring in this.state{ propertyinfo:''}我在 this.state{ propertyinfo:''} 中声明

when it should have been propertyinfo:[]当它应该是 propertyinfo:[]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM