wkentdag / spike-wordpress

wordpress plugin for spike
https://www.npmjs.com/package/spike-wordpress
Other
8 stars 0 forks source link

Can't retrieve properties of 'item' from within templates #92

Closed Synj24 closed 6 years ago

Synj24 commented 6 years ago

I'm not 100% sure if this is an issue with this plugin specifically as there is also a similar issue with the spike-datocms plugin.

When i'm trying to render my posts to a template:

app.js

plugins: [
    new Wordpress({
      site: 'www.accessaa.co.uk',
      addDataTo: locals,
      posts: [
        {
          name: 'posts',
          number: '10'
        },
        {
          name: 'news',
          category: 'news',
          order: 'date',
          template: {
            path: 'views/article.sgr',
            output: (item) => { return `posts/${item.slug}.html` }
          }
        },
        {
          name: 'features',
...

article.sgr

extends(src="layout.sgr")
  block(name="content")
    h1 {{ item.title }}

spike throws an error:

✗ ERROR
{ TypeError: Cannot read property 'title' of undefined
    at eval (eval at module.exports (C:\Users\mgregory\access-dev\current\access-all-areas\node_modules\reshape-code-gen\lib\scoped_eval.js:9:10), <anonymous>:1:156)
    at eval (eval at module.exports (C:\Users\mgregory\access-dev\current\access-all-areas\node_modules\reshape-code-gen\lib\scoped_eval.js:9:10), <anonymous>:1:170)
    at evalNode (C:\Users\mgregory\access-dev\current\access-all-areas\node_modules\reshape-eval-code\lib\index.js:27:70)
    at util.modifyNodes (C:\Users\mgregory\access-dev\current\access-all-areas\node_modules\reshape-eval-code\lib\index.js:21:40)
    at when.resolve.then (C:\Users\mgregory\access-dev\current\access-all-areas\node_modules\reshape-plugin-util\lib\index.js:26:29)
    at tryCatchReject (C:\Users\mgregory\access-dev\current\access-all-areas\node_modules\when\lib\makePromise.js:845:30)
    at runContinuation1 (C:\Users\mgregory\access-dev\current\access-all-areas\node_modules\when\lib\makePromise.js:804:4)
    at Fulfilled.when (C:\Users\mgregory\access-dev\current\access-all-areas\node_modules\when\lib\makePromise.js:592:4)
    at ContinuationTask.run (C:\Users\mgregory\access-dev\current\access-all-areas\node_modules\when\lib\makePromise.js:702:24)
    at Scheduler._drain (C:\Users\mgregory\access-dev\current\access-all-areas\node_modules\when\lib\Scheduler.js:62:19) id: '4fb453d3' }

I've tried to JSON.stringify(item) also but this returns undefined

Heres a link to my repo incase you wanted to reproduce this error.

Apologies if this issue is unrelated to your plugin. Thanks :^)

wkentdag commented 6 years ago

I'm guessing this is a problem with your data rather than the library. I have a test for this use case that has pretty much the same template.

I was poking around the raw API endpoint that your data is getting pulled from, and it looks like you don't have any posts that have the category news. So I would just assume that the API is not returning anything, which would explain why spike throws an error when trying to access the title prop of an item which doesn't exist. Hope that helps!

Synj24 commented 6 years ago

It's rendering the posts else where though. I'm aware the slug is different from the name and i thought this might be causing problems but i also tried with features and still no luck.

{
  "found": 3832,
  "posts": [
    {
    ...
      "categories": {
        ...
        "Latest News": {
          "ID": 2,
          "name": "Latest News",
          "slug": "news",
          "description": "The latest news from the live event production world",
          "post_count": 3154,
          "meta": {
            "links": {
              "self": "https:\/\/public-api.wordpress.com\/rest\/v1\/sites\/93531691\/categories\/slug:news",
              "help": "https:\/\/public-api.wordpress.com\/rest\/v1\/sites\/93531691\/categories\/slug:news\/help",
              "site": "https:\/\/public-api.wordpress.com\/rest\/v1\/sites\/93531691"
            }
          },
          ...
"categories": {
        "Features": {
          "ID": 3,
          "name": "Features",
          "slug": "features",
          "description": "All the in-depth AAA features",
          "post_count": 248,
          "meta": {
            "links": {
              "self": "https:\/\/public-api.wordpress.com\/rest\/v1\/sites\/93531691\/categories\/slug:features",
              "help": "https:\/\/public-api.wordpress.com\/rest\/v1\/sites\/93531691\/categories\/slug:features\/help",
              "site": "https:\/\/public-api.wordpress.com\/rest\/v1\/sites\/93531691"
            }
          },
          "parent": 0
        },

Thanks for your help anyway :bowing_man: