Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

Troubleshooting Eleventy and Sanity integration, TypeError with getTime function, resolved with uncertain cause

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
🙂
Feb 25, 2022, 3:25 PM
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
Feb 25, 2022, 7:42 PM
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
Feb 25, 2022, 7:43 PM
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
Feb 25, 2022, 7:42 PM
Hello also Julia 🙂! Thank you so much! I'm working on trying everything, it really helped me get going
Feb 26, 2022, 11:09 AM
It was not related to dates at all unfortunately but to the alias used I think. So, sorta solved but not completely sure how.
Feb 26, 2022, 12:07 PM
It was not related to dates at all unfortunately but to the alias used I think. So, sorta solved but not completely sure how.
Feb 26, 2022, 12:07 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?