TypeError: Cannot read properties of undefined (reading "labels")

  • freemiumstuff.netlify.app
  • APIs are working perfectly on local server.
  • There is no ‘Undefined’ error in the local server
    /// Build logs
    1:18:35 PM: Netlify Build
    1:18:35 PM: ────────────────────────────────────────────────────────────────
    1:18:35 PM: ​
    1:18:35 PM: ❯ Version
    1:18:35 PM: @netlify/build 29.21.1
    1:18:35 PM: ​
    1:18:35 PM: ❯ Flags
    1:18:35 PM: baseRelDir: true
    1:18:35 PM: buildId: 6513de34f1e8a96a06c31973
    1:18:35 PM: deployId: 6513de34f1e8a96a06c31975
    1:18:35 PM: ​
    1:18:35 PM: ❯ Current directory
    1:18:35 PM: /opt/build/repo
    1:18:35 PM: ​
    1:18:35 PM: ❯ Config file
    1:18:35 PM: No config file was defined: using default values.
    1:18:35 PM: ​
    1:18:35 PM: ❯ Context
    1:18:35 PM: production
    1:18:35 PM: ​
    1:18:35 PM: Build command from Netlify app
    1:18:35 PM: ────────────────────────────────────────────────────────────────
    1:18:35 PM: ​
    1:18:35 PM: $ astro build
    1:18:36 PM: 07:48:36 AM [content] No content directory found. Skipping type generation.
    1:18:36 PM: 07:48:36 AM [build] output target: static
    1:18:36 PM: 07:48:36 AM [build] Collecting build info…
    1:18:36 PM: 07:48:36 AM [build] Completed in 80ms.
    1:18:36 PM: 07:48:36 AM [build] Building static entrypoints…
    1:18:37 PM: 07:48:37 AM [build] Completed in 1.31s.
    1:18:37 PM:
    1:18:37 PM: building client
    1:18:37 PM: Completed in 230ms.
    1:18:37 PM:
    1:18:37 PM:
    1:18:37 PM: generating static routes
    1:18:37 PM: :arrow_forward: src/pages/index.astro
    1:18:37 PM: {
    1:18:37 PM: PUBLIC_URL: “",
    1:18:37 PM: BASE_URL: “/”,
    1:18:37 PM: MODE: “production”,
    1:18:37 PM: DEV: false,
    1:18:37 PM: PROD: true,
    1:18:37 PM: SSR: true,
    1:18:37 PM: SITE: undefined,
    1:18:37 PM: ASSETS_PREFIX: undefined,
    1:18:37 PM: CONTENT_API_KEY: "
    1:18:37 PM: _: “/opt/build/repo/node_modules/.bin/astro”,
    1:18:37 PM: URL: “https://freemiumstuff.netlify.app
    1:18:37 PM: }
    1:18:37 PM: file:///opt/build/repo/dist/chunks/pages/index_5ed64f43.mjs:68
    1:18:37 PM: return renderTemplate${renderComponent($$result, "BaseLayout", $$BaseLayout, { "title": "Freemium Stuff" }, { "default": ($$result2) => renderTemplate${maybeRenderHead()}${renderComponent($$result2, “HeroAnimation”, $$HeroAnimation, {})}${renderComponent($$result2, “SliderSec”, $$SliderSec, { “heading”: “Free Courses” }, { “default”: ($$result3) => renderTemplate${renderComponent($$result3, "SplideWrapper", $$SplideWrapper, {}, { "default": ($$result4) => renderTemplate${allPosts.map((post) => post.labels.includes(apiParams.labels.freeCourses) && renderTemplate${renderComponent($$result4, "SliderCard", $$SliderCard, { "class": "splide__slide", "stuffThumbnail": post.images[0]?.url, "stuffLink": post.url, "stuffAlt": post.title })})} })} })}${renderComponent($$result2, “SliderSec”, $$SliderSec, { “heading”: “Free E-Books” }, { “default”: ($$result3) => renderTemplate${renderComponent($$result3, "SplideWrapper", $$SplideWrapper, {}, { "default": ($$result4) => renderTemplate${allPosts.map((post) => post.labels.includes(apiParams.labels.freeEbooks) && renderTemplate${renderComponent($$result4, "SliderCard", $$SliderCard, { "class": "splide__slide", "stuffThumbnail": post.images[0]?.url, "stuffLink": post.url, "stuffAlt": post.title })})} })} })}${renderComponent($$result2, “SliderSec”, $$SliderSec, { “heading”: “Free Cheat Sheets” }, { “default”: ($$result3) => renderTemplate${renderComponent($$result3, "SplideWrapper", $$SplideWrapper, {}, { "default": ($$result4) => renderTemplate${allPosts.map((post) => post.labels.includes(apiParams.labels.freeCheatSheets) && renderTemplate${renderComponent($$result4, "SliderCard", $$SliderCard, { "class": "splide__slide", "stuffThumbnail": post.images[0]?.url, "stuffLink": post.url, "stuffAlt": post.title })})} })} })}${renderComponent($$result2, “SliderSec”, $$SliderSec, { “heading”: “Articles” }, { “default”: ($$result3) => renderTemplate${renderComponent($$result3, "SplideWrapper", $$SplideWrapper, {}, { "default": ($$result4) => renderTemplate${allPosts.map((post) => post.labels.includes(apiParams.labels.Articles) && renderTemplate${renderComponent($$result4, "SliderCard", $$SliderCard, { "class": "splide__slide", "stuffThumbnail": post.images[0]?.url, "stuffLink": post.url, "stuffAlt": post.title })})} })} })} })};
    1:18:37 PM: TypeError: Cannot read properties of undefined (reading “labels”)
    1:18:37 PM: at file:///opt/build/repo/dist/chunks/pages/index_5ed64f43.mjs:68:1087
    1:18:37 PM: at Array.map ()
    1:18:37 PM: at Object.default (file:///opt/build/repo/dist/chunks/pages/index_5ed64f43.mjs:68:1068)
    1:18:37 PM: at new AstroComponentInstance (file:///opt/build/repo/dist/chunks/astro_e6abcb9f.mjs:861:32)
    1:18:37 PM: at createAstroComponentInstance (file:///opt/build/repo/dist/chunks/astro_e6abcb9f.mjs:899:20)
    1:18:37 PM: at renderAstroComponent (file:///opt/build/repo/dist/chunks/astro_e6abcb9f.mjs:1275:20)
    1:18:37 PM: at renderComponent (file:///opt/build/repo/dist/chunks/astro_e6abcb9f.mjs:1294:12)
    1:18:37 PM: at Object.default (file:///opt/build/repo/dist/chunks/pages/index_5ed64f43.mjs:68:949)
    1:18:37 PM: at new AstroComponentInstance (file:///opt/build/repo/dist/chunks/astro_e6abcb9f.mjs:861:32)
    1:18:37 PM: at createAstroComponentInstance (file:///opt/build/repo/dist/chunks/astro_e6abcb9f.mjs:899:20)
    1:18:37 PM: Node.js v18.18.0
    1:18:37 PM: ​
    1:18:37 PM: “build.command” failed
    1:18:37 PM: ────────────────────────────────────────────────────────────────
    1:18:37 PM: ​
    1:18:37 PM: Error message
    1:18:37 PM: Command failed with exit code 1: astro build
    1:18:37 PM: ​
    1:18:37 PM: Error location
    1:18:37 PM: In Build command from Netlify app:
    1:18:37 PM: astro build
    1:18:37 PM: ​
    1:18:37 PM: Resolved config
    1:18:37 PM: build:
    1:18:37 PM: command: astro build
    1:18:37 PM: commandOrigin: ui
    1:18:37 PM: environment:
    1:18:37 PM: - BLOG_ID
    1:18:37 PM: - CONTENT_API_KEY
    1:18:37 PM: - PUBLIC_URL
    1:18:37 PM: publish: /opt/build/repo/dist
    1:18:37 PM: publishOrigin: ui
    1:18:39 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
    1:18:39 PM: Failing build: Failed to build site
    1:18:39 PM: Finished processing build request in 18.827s
    1:18:39 PM: Failed during stage “building site”: Build script returned non-zero exit code: 2

Hi @bhaveshxrawat , thanks for the post and welcome.

As you can see from the quoted error post.labels throws an error. The post data either does not have a property called labels, or the post data is not an object, or the post data is undefined.

Kindly console.log the allPosts data to make sure the structure matches what you expect and then work your way up from there.


1 Like

Hi, @clarnx

Thank you for answering!

As I mentioned, the API works perfectly on the local server and serves all the assets. No console errors. The allPosts is an array, and post is an object. Is there any way I could be sure of what exactly is the issue. Have already spent way too much resource and time on this issue.

Please help.

**UPDATE: Tried to deploy it on Github pages, and it built without any problem and any code change on the first run.

But, I want my project live on Netlify, not GHP

Hi @bhaveshxrawat , if possible can you share a code snippet or a repository in order for me to help with the debugging.

Sure. Here is the repo

Hi @bhaveshxrawat , the reason why you are having build errors is that all environment variable values in your .env file is undefined when your try to access the value.
This happens because by default .env or .env.production files are added to the .gitignore file hence it is not deployed to GitHub.
Once it is not on GitHub when you are deploying of Netlify environment variables are undefined.

If you want the environment variables to work you need to set the same values in your .env file on Netlify using the Netlify UI Dashboard.
Kindly check the Netlify Documentation link below on how to set environment variables on Netlify.

You can also check the GitHub issues page below regarding the same problem.

Make the changes and then redeploy to see if it works.
Let me know the outcome.


Yes, I’m aware of this, and I already have set the environment variables in my Netlify Dashboard.

I’m sure that the issue I’m having is not due to those. Coz they would look like this:

I have solved it. Thanks for the help so far!

1 Like

Hi @bhaveshxrawat , thanks for the update on resolving the issue.