site
site copied to clipboard
Site variables documentation is wrong
This part of the documentation in https://hexo.io/docs/variables#Site-Variables page is wrong :
See this related issue https://github.com/hexojs/hexo/issues/3038
Actually site.posts
or site.pages
are not arrays returning posts or pages, you actually have to call site.posts.data
or site.pages.data
. I took me days to figure that out, I had the exact same problem as https://github.com/hexojs/hexo/issues/3038
Nope.
site.posts
, site.pages
(etc.) actually return a Query
object, which contains an array of Document
object. data
is a private property of Query
and anyone except hexo's projects should NEVER access that property directly.
So same as in #3038 how do I manage the returned (query) object?
Because the answer in #3038 is to call .data
and it works but when I call only page.posts
and not page.posts.data
I have an inerrable object which is not an array where none of the post or page methods are available.
each po in page.posts.data
p
time(datetime=date_xml(po.date) itemprop="datePublished")
= date(po.date)
span
= ' | ' + po.title
The docs say site.pages
should return an array of page objects but it doesn't! I can't do this :
each po in page.posts
p = po.title
So ok page.posts
return a Query objects but this mean the doc is incorrect and must be changed and tell me how to deal with the query object please?
I'm not a JS dev and I find it especially complex to debug JS because there is no inspection methods like var_dump
in PHP or .class
and .methods
in ruby. Usually JS people tell you to put the object in a console.log()
but this work only for client side dev in a browser.
The thing is I'm not a JS expert, I wanted to build a theme I referred to the doc, and when reading the actual doc I expect this code to work and actually it doesn't.
each po in page.posts
p = po.title
I wasn't aware of this warehouse doc (https://hexojs.github.io/warehouse/) by reading this I understand that the easiest solution for my issue is:
each po in page.posts.toArray()
p
time(datetime=date_xml(po.date) itemprop="datePublished")
= date(po.date)
span
= ' | ' + po.title
Is this the right way to do? I don't need to call an enumerator function like map or each since I'm already using each pug iterator that will work fine with an array.
I'll PR the doc to fix it.
page.posts.toArray()
warehouse
already provided forEach
, sort
, find
, findOne
and many other util implementation, why use JavaScript built-in Array method instead?
warehouse
already providedforEach
,sort
,find
,findOne
and many other util implementation, why use JavaScript built-in Array method instead?
IDK I saw toArray listed on the warehouse and I need an array in the end, sort return a query object for example. I ended up with something like that each po in page.posts.sort('date', -1).toArray()
. But as I said I'm not JS dev so I may not take the best choices.