简体   繁体   中英

How to fix this TypeError

TypeError: Cannot read properties of undefined (reading '0'). How do I fix this type error that is occuring with the dagID = result[0].dag_id line

 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!!!

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 might be an empty array so result[0] returns undefined

you can use optional chaining to have fallback id if result array is empty:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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