简体   繁体   English

如何修复此类型错误

[英]How to fix this TypeError

TypeError: Cannot read properties of undefined (reading '0'). TypeError:无法读取未定义的属性(读取“0”)。 How do I fix this type error that is occuring with the dagID = result[0].dag_id line如何修复 dagID = result[0].dag_id 行发生的这种类型错误

 async function accessData(){ const access = await main(); const result = access.dag_runs.map(file => ({start_date: file.start_date, end_date: file.end_date, state: file.state, dag_run_id: file.dag_run_id, dag_id: file.dag_id})) console.log(result); /*await bqConnection() .dataset('IPP_SLA') .table('sla_table') .insert(result);*/ console.log(`Inserted ${result.length} rows`) } function determineCron(result){ dagID = result[0].dag_id job = bqConnection().query(`SELECT * FROM \`np-inventory-planning-thd.IPP_SLA.expected_sla\` where dag_id = "${dagID}"`) cronTime = job[0].cron_time var interval = parser.parseExpression(cronTime); console.log('Date: ', interval.next().toString()); } determineCron()

The determineCron() function have to take an argument that will be an array!!! determineCron()函数必须接受一个参数,该参数将是一个数组!!!

Here are the things you might take into consideration:以下是您可能会考虑的事项:

  • Make sure that result type is an array you can use Array.isArray method of Array prototype to check it确保result类型是一个数组,你可以使用Array 原型Array.isArray方法来检查它
  • result might be an empty array so result[0] returns undefined result可能是一个空数组,因此result[0]返回undefined

you can use optional chaining to have fallback id if result array is empty:如果result数组为空,您可以使用可选链接来获得后备 ID:

    dagID = result?.[0]?.dag_id || 0

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

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