Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read properties of undefined (reading 'length') #9

Closed
mziyut opened this issue Jan 6, 2023 · 1 comment · Fixed by #7
Closed

TypeError: Cannot read properties of undefined (reading 'length') #9

mziyut opened this issue Jan 6, 2023 · 1 comment · Fixed by #7

Comments

@mziyut
Copy link

mziyut commented Jan 6, 2023

What

It seems that variables in repoData are sometimes undefined

const repoData = await Promise.all(
repoList.map(async (repo) => {
const url = `https://circleci.com/api/v2/project/gh/${repo}/envvar`;
const results = await fetch(url, {
headers: { "Circle-Token": `${CIRCLE_TOKEN}` },
}).then((res) => res.json());
return {
name: repo,
variables: results.items,
};
})
);
USER_DATA.projects = repoData.filter((repo) => repo.variables.length > 0);

Log

file:///project/index.js:150
USER_DATA.projects = repoData.filter((repo) => repo.variables.length > 0);
                                                              ^

TypeError: Cannot read properties of undefined (reading 'length')
    at file:///project/index.js:150:63
    at Array.filter (<anonymous>)
    at file:///project/index.js:150:31
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v19.3.0
@mziyut
Copy link
Author

mziyut commented Jan 6, 2023

I patched it and got around it.

diff --git a/index.js b/index.js
index 7bc2df2..5b3ce33 100644
--- a/index.js
+++ b/index.js
@@ -146,6 +146,6 @@ const repoData = await Promise.all(
     };
   })
 );
-USER_DATA.projects = repoData.filter((repo) => repo.variables.length > 0);
+USER_DATA.projects = repoData.filter((repo) => (repo.variables || []).length > 0);
 
 console.dir(USER_DATA, { depth: null });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant