thinhle-agilityio/gatsby-source-apiserver

Cannot read post of undefined

Closed this issue · 4 comments

My config to the issue stated in the title:

`{
resolve: 'gatsby-source-apiserver',
options: {
// Type prefix of entities from server
typePrefix: 'internal__',

    // The url, this should be the endpoint you are attempting to pull data from
    url: 'http://localhost:8010/proxy/companies',

    method: 'POST',

    headers: {
      Accept: 'application/json',
      'user-key': 'cc9c2b79c9b903c7e5c5dd721c77fb17',
    },

    // Request body
    data: { 'fields name,published.name,published.cover.url,published.slug;where id = 51;' },

    // Name of the data to be downloaded.  Will show in graphQL or be saved to a file
    // using this name. i.e. posts.json
    name: 'posts',

    // Nested level of entities in response object, example: `data.posts`
    entityLevel: 'data.posts',

    // Define schemaType to normalize blank values
    // example:
    // const postType = {
    //   id: 1,
    //   name: 'String',
    //   published: true,
    //   object: {a: 1, b: '2', c: false},
    //   array: [{a: 1, b: '2', c: false}]
    // }
    // schemaType: postType,

    // Simple authentication, if optional, set it null
    auth: {
      username: null,
      password: null,
    },

    // Optionally save the JSON data to a file locally
    // Default is false
    localSave: true,

    //  Required folder path where the data should be saved if using localSave option
    //  This folder must already exist
    path: `${__dirname}/src/data/`,

    // Optionally include some output when building
    // Default is false
    verboseOutput: true, // For debugging purposes

    // Optionally skip creating nodes in graphQL.  Use this if you only want
    // The data to be saved locally
    // Default is false
    skipCreateNode: false, // skip import to graphQL, only use if localSave is all you want
  },
},`

`TypeError: Cannot read property 'posts' of undefined

  • helpers.js:6 exports.objectRef
    [gddb]/[gatsby-source-apiserver]/helpers.js:6:14

  • gatsby-node.js:90
    [gddb]/[gatsby-source-apiserver]/gatsby-node.js:90:22

  • Generator.next

  • gatsby-node.js:1 step
    [gddb]/[gatsby-source-apiserver]/gatsby-node.js:1:253

  • gatsby-node.js:1
    [gddb]/[gatsby-source-apiserver]/gatsby-node.js:1:423

  • task_queues.js:89 processTicksAndRejections
    internal/process/task_queues.js:89:5`

Obviously missing a puzzle piece here or not having a wrong config, any hint would be appreciated. Cheers

@drood87 the entityLevel: 'data.posts' should match your API response body. Can you use tool like Postman to post your request above and see what it response?

Sure! So the response looks like this,

[ { "id": 51, "developed": [ 120, 123, 129, 130, 132, 133, 227, 228, 229, 230, 239, 452, 453, 455, 456, 457, 458, 1253, 1279, 1332, 3157, 3182, 7313, 8173, 11355, 11593, 11704, 19162, 21971, 21973, 21974, 21975, 21976, 21977, 25601, 25683, 27429, 38659, 44544, 47452, 52016, 75379, 75380, 76152, 96374, 105125, 111650, 111652, 111655 ], "logo": 29, "name": "Blizzard Entertainment", "published": [ { "id": 120, "cover": { "id": 914, "image_id": "hohtqw21z8ucbsylj7i1" }, "first_release_date": 1337040000, "name": "Diablo III", "slug": "diablo-iii" }, ] } ]

What would be the correct entity level then? Sorry if my question sounds stupid and I had to open an issue for it. I commented that part out and it worked fine! I'm quite new to this. Thanks in advance

I think you don't need entityLevel since your API response return what you want as root array. So just remove params entityLevel .

Thanks for responding and sorry for me taking so long :D Yeah I just disabled it and it's working fine! Thank you and thanks for that nice plugin! :)