Troubleshooting Eleventy and Sanity integration, TypeError with getTime function, resolved with uncertain cause
6 replies
Last updated: Feb 26, 2022
J
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
this seems to be dependent on the pagination, I think? After some googling. Any advice appreciated
🙂
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
🙂
Feb 25, 2022, 3:25 PM
J
hmmm I’m just learning some 11ty lately and using the sanity blog starter for reference.
where are you using the
try either adding
Perhaps also try Date.now as an alternative to getTime, might perform better with the same return:
where are you using the
getTimefunction? 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
definedor
!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()))
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
J
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
J
hmmm I’m just learning some 11ty lately and using the sanity blog starter for reference.
where are you using the
try either adding
Perhaps also try Date.now as an alternative to getTime, might perform better with the same return:
where are you using the
getTimefunction? 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
definedor
!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()))
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
J
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
J
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
J
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.