Pagination error "Cannot read property 'getTime' of undefined" in Eleventy

6 replies
Last updated: Feb 26, 2022
Hi again, I'm trying to create a content type called pages (from sanity+eleventy base template), I've got it to work on the sanity side and they show up in the studio but when I make adjustments in the eleventy-associated files I run into trouble. Basically I tried to just copy all the post.njk-files and then replace the names in them with 'page' or 'pages' depending on context. When I reached the top level page.njk I get
 Cannot read property 'getTime' of undefined
sanity-eleventy-blog-web: `TypeError` was thrown:
sanity-eleventy-blog-web:     TypeError: Cannot read property 'getTime' of undefined
sanity-eleventy-blog-web:         at sortFunctionDateInputPath
this seems to be dependent on the pagination, I think? After some googling. Any advice appreciated
🙂
AI Update

This error is happening because your new "pages" content type likely doesn't have a publishedAt date field (or whatever date field the sortFunctionDateInputPath is trying to access), but the Eleventy template code you copied is still trying to sort by that date.

When you copied the post.njk files and renamed them for pages, the template is still expecting date properties that exist on blog posts but might not exist on your pages. The getTime() method is being called on undefined because the date field doesn't exist.

Here's how to fix it:

Option 1: Add a date field to your pages schema

In your Sanity Studio schema for pages, make sure you have a publishedAt field (or whatever field name the sort function expects):

{
  name: 'page',
  type: 'document',
  fields: [
    // ... other fields
    {
      name: 'publishedAt',
      type: 'datetime',
      title: 'Published at',
    }
  ]
}

Option 2: Modify the sort function in your Eleventy config

If pages don't need dates (which makes sense for static pages), you'll need to modify the pagination configuration. Look in your .eleventy.js or eleventy.config.js file for where sortFunctionDateInputPath is defined, and create a different sort function for pages that doesn't rely on dates:

// For pages, sort by title or slug instead of date
eleventyConfig.addCollection("pages", function(collectionApi) {
  return collectionApi.getAll()
    .filter(item => item.data.contentType === 'page')
    .sort((a, b) => {
      // Sort alphabetically by title or slug
      return a.data.title.localeCompare(b.data.title);
    });
});

Option 3: Make the sort function handle missing dates

Find the sortFunctionDateInputPath function and add a null check:

function sortFunctionDateInputPath(a, b) {
  const dateA = a.publishedAt || a.data.publishedAt;
  const dateB = b.publishedAt || b.data.publishedAt;
  
  if (!dateA) return 1;  // Push items without dates to the end
  if (!dateB) return -1;
  
  return dateB.getTime() - dateA.getTime();
}

The most likely culprit is in your pagination configuration in the frontmatter of your page.njk file. Look for something like before: sortFunctionDateInputPath and either remove it or change it to a sort function that works for pages without dates.

Show original thread
6 replies
hmmm I’m just learning some 11ty lately and using the sanity blog starter for reference.
where are you using the
getTime
function? At a glance it looks like where this field is being referenced expects only a number or date value returned. If you have ‘dirty’ data where some documents have a date/time field that could be a string, null, or unknown - that could cause this error.
try either adding
defined
or
!
as a filter to check if that field exists - or filter any value that isn’t a number to your query. This example may help:
(defined(unPublishDate) && dateTime(unPublishDate) >= dateTime(now()))
Perhaps also try Date.now as an alternative to getTime, might perform better with the same return:

const currentTime = Date.now()
const newDate = new Date()

console.log('Date.now: ', currentTime)

console.log('getTime: ', newDate.getTime())

// returns
// 'Date.now: ' 1645817834960
// 'getTime: ' 1645817834960
if you would like a mostly untouched example of the starter from the sanity site, you’re welcome to reference mine: https://github.com/julesjazz/example-11tyblog-netlify
hmmm I’m just learning some 11ty lately and using the sanity blog starter for reference.
where are you using the
getTime
function? At a glance it looks like where this field is being referenced expects only a number or date value returned. If you have ‘dirty’ data where some documents have a date/time field that could be a string, null, or unknown - that could cause this error.
try either adding
defined
or
!
as a filter to check if that field exists - or filter any value that isn’t a number to your query. This example may help:
(defined(unPublishDate) && dateTime(unPublishDate) >= dateTime(now()))
Perhaps also try Date.now as an alternative to getTime, might perform better with the same return:

const currentTime = Date.now()
const newDate = new Date()

console.log('Date.now: ', currentTime)

console.log('getTime: ', newDate.getTime())

// returns
// 'Date.now: ' 1645817834960
// 'getTime: ' 1645817834960
Hello also Julia 🙂! Thank you so much! I'm working on trying everything, it really helped me get going
It was not related to dates at all unfortunately but to the alias used I think. So, sorta solved but not completely sure how.
It was not related to dates at all unfortunately but to the alias used I think. So, sorta solved but not completely sure how.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?