简体   繁体   English

无法弄清楚我做错了什么。 未处理的拒绝(TypeError):无法读取未定义的属性“检查”

[英]Cant figure out what Im doing wrong. Unhandled Rejection (TypeError): Cannot read property 'inspection' of undefined

so I have a eventhandler that calls for data from an Api.所以我有一个事件处理程序,它需要来自 Api 的数据。 Now Im writing a function that formats the incoming data.现在我正在编写一个格式化输入数据的 function。

eventHandlers={{
            dragend : async (e)=>{
              const lat = e.target._latlng.lat;
              const lng = e.target._latlng.lng;
              const result = await callSodaApi(lat, lng);
              const data = consolidate(result.data);   <<<<<<<<<<<<<<<-
              console.log(data);
              setMapConfig({
                markerCoord : [lat, lng],
                queryData : result.data
              });
            },
          }}

the incoming data looks like this: this example before is from the same restaurant, but could also be a different restaurant differentiate by the key camis.传入的数据如下所示:之前的示例来自同一家餐厅,但也可能是不同的餐厅,通过关键 camis 进行区分。

{
  action: "D"
  boro: "Manhattan"
  building: "625"
  camis: "40363298"
  critical_flag: "G"
  cuisine_description: "American"
  dba: "CAFE METRO"
  grade: "A"
  grade_date: "2016-05-13T00:00:00.000"
  inspection_date: "2016-05-13T00:00:00.000"
  inspection_type: "Cycle Inspection / Initial Inspection"
  latitude: "40.756185423077"
  longitude: "-73.990564733785"
  record_date: "2021-05-29T06:03:39.000"
  score: "4"
  street: "8 AVENUE"
  violation_code: "10E"
  violation_description: "Accurate thermometer not provided in refrigerated or hot holding equipment."
  zipcode: "10018"
},
{
  action: "U"
  boro: "Manhattan"
  building: "625"
  camis: "40363298"
  critical_flag: "C"
  cuisine_description: "American"
  dba: "CAFE METRO"
  grade: "A"
  grade_date: "2017-10-10T00:00:00.000"
  inspection_date: "2017-10-10T00:00:00.000"
  inspection_type: "Cycle Inspection / Re-inspection"
  latitude: "40.756185423077"
  longitude: "-73.990564733785"
  phone: "2127149342"
  record_date: "2021-05-29T06:03:39.000"
  score: "12"
  street: "8 AVENUE"
  violation_code: "04H"
  violation_description: "Raw, cooked or prepared food is adulterated, contaminated, cross-contaminated, or not discarded in accordance with HACCP plan."
  zipcode: "10018"
}

if they are the same restaurant, I want to combine the different violations from the same restaurant under the same object with new violation key.如果他们是同一家餐厅,我想将同一家餐厅的不同违规行为与新的违规密钥结合在同一个 object 下。 Like the code below.就像下面的代码。

{
  action: "U"
  boro: "Manhattan"
  building: "625"
  camis: "40363298"
  critical_flag: "C"
  cuisine_description: "American"
  dba: "CAFE METRO"
  grade: "A"
  grade_date: "2017-10-10T00:00:00.000"
  latitude: "40.756185423077"
  longitude: "-73.990564733785"
  phone: "2127149342"
  record_date: "2021-05-29T06:03:39.000"
  score: "12"
  street: "8 AVENUE"
  violation: [{
        date: "2016-05-13T00:00:00.000"
        type: "Cycle Inspection / Initial Inspection"
        violation_code: "04H"
  violation_description: "Raw, cooked or prepared food is adulterated, contaminated, cross-contaminated, or not discarded in accordance with HACCP plan."/
         },{
        date: "2017-10-10T00:00:00.000"
        type: "Cycle Inspection / Re-inspection"
        violation_code: "04H"
        violation_description: "Raw, cooked or prepared food is adulterated, contaminated, cross-contaminated, or not discarded in accordance with HACCP plan."
}]
  zipcode: "10018"
}

this is the function I wrote,这是我写的 function,

const consolidate = (dataArray) => {
  let a = 0;
  let nxt = 1;
  let holdingArray = [];

  while ( nxt < dataArray.length-1 ) {
    let newObj;
    // if same restaurant
    if ( dataArray[a].camis === dataArray[nxt].camis ) {
    // when this is the first iteration of restaruant 
      newObj = nxt - a === 1
        ? {
            camis: dataArray[a].camis,
            dba: dataArray[a].dba,
            cuisine: dataArray[a].cuisine_description,
            building: dataArray[a].building,
            street: dataArray[a].street,
            boro: dataArray[a].boro,
            zipcode: dataArray[a].zipcode,
            grade : dataArray[a].grade || dataArray[nxt].grade,
            latitude: dataArray[a].latitude,
            longitude: dataArray[a].longitude,
            inspection: 
              [ {
                date: dataArray[a].inspection_date,
                type: dataArray[a].inspection_type,
                violation_code: dataArray[a].violation_code,
                violation_description: dataArray[a].violation_description

              }, {
                date: dataArray[nxt].inspection_date,
                type: dataArray[nxt].inspection_type,
                violation_code: dataArray[nxt].violation_code,
                violation_description: dataArray[nxt].violation_description
              }]
        }
        : {
            camis: dataArray[a].camis,
            dba: dataArray[a].dba,
            cuisine: dataArray[a].cuisine_description,
            building: dataArray[a].building,
            street: dataArray[a].street,
            boro: dataArray[a].boro,
            zipcode: dataArray[a].zipcode,
            grade : dataArray[a].grade || dataArray[nxt].grade,
            latitude: dataArray[a].latitude,
            longitude: dataArray[a].longitude,
            inspection: [...newObj.inspection , {
                date: dataArray[nxt].inspection_date,
                type: dataArray[nxt].inspection_type,
                violation_code: dataArray[nxt].violation_code,
                violation_description: dataArray[nxt].violation_description
            }]
        };
        console.log(newObj) <<<<<<<<<<
        nxt ++; 
    } else {
      holdingArray.push(newObj);
      a = nxt;
      nxt = nxt + 1;
    };
  };

  return holdingArray;
};

I'm able to see the very first iteration from the console.log() in the console.我可以从控制台中的 console.log() 看到第一次迭代。 But right after I would get a type error但是就在我收到类型错误之后

在此处输入图像描述

Can someone help me understand what I'm doing wrong please.有人可以帮我理解我做错了什么吗? And Thank you for your time.谢谢你的时间。

The faulty line is being pointed out in the error message.错误消息中指出了故障线路。 You are trying to access inspection on newObj before it was assigned.您正在尝试在分配之前访问对newObjinspection

For a better understanding, I've reproduced your error in this minimal code:为了更好地理解,我在这个最小的代码中重现了你的错误:

 let newObj; newObj = { inspection: newObj.inspection // newObj is still undefined at this point }

I thought I'd take a stab at your logic using reduce .我想我会使用reduce来测试你的逻辑。 It sets up a violation array for each object for consistency, and reduces the codebase considerably.它为每个 object 设置了一个violation数组以保持一致性,并大大减少了代码库。 If you're refining the objects beyond the violation array, that's pretty easy to add to this as well.如果您要提炼超出冲突数组的对象,那么添加到此也很容易。

 let odata = [{ action: "D", boro: "Manhattan", building: "625", camis: "40363298", critical_flag: "G", cuisine_description: "American", dba: "CAFE METRO", grade: "A", grade_date: "2016-05-13T00:00:00.000", inspection_date: "2016-05-13T00:00:00.000", inspection_type: "Cycle Inspection / Initial Inspection", latitude: "40.756185423077", longitude: "-73.990564733785", record_date: "2021-05-29T06:03:39.000", score: "4", street: "8 AVENUE", violation_code: "10E", violation_description: "Accurate thermometer not provided in refrigerated or hot holding equipment.", zipcode: "10018" }, { action: "U", boro: "Manhattan", building: "625", camis: "40363298", critical_flag: "C", cuisine_description: "American", dba: "CAFE METRO", grade: "A", grade_date: "2017-10-10T00:00:00.000", inspection_date: "2017-10-10T00:00:00.000", inspection_type: "Cycle Inspection / Re-inspection", latitude: "40.756185423077", longitude: "-73.990564733785", phone: "2127149342", record_date: "2021-05-29T06:03:39.000", score: "12", street: "8 AVENUE", violation_code: "04H", violation_description: "Raw, cooked or prepared food is adulterated, contaminated, cross-contaminated, or not discarded in accordance with HACCP plan.", zipcode: "10018" } ] let newdata = odata.reduce((final, obj) => { let vobj = { date: obj.inspection_date, type: obj.inspection_type, violation_code: obj.violation_code, violation_description: obj.violation_description } if (final[obj.camis]) final[obj.camis].violation.push(vobj) else final[obj.camis] = {...obj, violation: [vobj] } return final; }, {}) console.log(Object.values(newdata))

暂无
暂无

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

相关问题 未处理的拒绝(TypeError):无法读取未定义的属性 - Unhandled Rejection (TypeError): Cannot read property of undefined 未处理的拒绝(TypeError):在执行React教程时无法读取未定义的属性“ map” - Unhandled Rejection (TypeError): Cannot read property 'map' of undefined when doing React tutorial 未处理的拒绝(TypeError):无法读取未定义的属性“地图”......无法获取所有帖子 - Unhandled Rejection (TypeError): Cannot read property 'map' of undefined .......cant get all posts 反应 - Redux | 未处理的拒绝(类型错误):无法读取未定义的属性“数据” - React - Redux | Unhandled Rejection (TypeError): Cannot read property 'data' of undefined 未处理的拒绝(TypeError):无法读取未定义的属性“ id” - Unhandled Rejection (TypeError): Cannot read property 'id' of undefined Reactjs + Redux 未处理的拒绝(TypeError):无法读取未定义的属性“数据” - Reactjs + Redux Unhandled Rejection (TypeError): Cannot read property 'data' of undefined 未处理的拒绝(类型错误):无法读取未定义的属性“推送” - Unhandled Rejection (TypeError): Cannot read property 'push' of undefined React-未处理的拒绝(TypeError):无法读取未定义的属性“数据” - React- Unhandled Rejection (TypeError): Cannot read property 'data' of undefined 未处理的承诺拒绝:TypeError:无法读取未定义的属性“用户名” - Unhandled promise rejection: TypeError: Cannot read property 'username' of undefined 未处理的拒绝(TypeError):无法读取未定义的属性“图像” - Unhandled Rejection (TypeError): Cannot read property 'image' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM