fvdm / nodejs-openkvk

Unofficial Openkvk.nl module for Node.js
https://www.npmjs.com/package/openkvk
The Unlicense
2 stars 0 forks source link

NodeJS OpenKvk returns SyntaxError: Unexpected token < #13

Closed ErikHoeven closed 7 years ago

ErikHoeven commented 7 years ago

Hello i am trying to get results from the OpenKvk Node API, avalable in the npm libary. I use the example which is given on the website: https://frankl.in/code/openkvk-voor-node-js

I register on the webiste: https://overheid.io and make an API Key which delivers me a token number which i used in the code

Which provide me the folowing code:

var openkvk = require ('openkvk') ({
    apikey: '[TokenKey come's here]'
});

   openkvk( 'ahold', function( err, data ) {
                if( err ) { return console.log( err )}
                data.forEach( function( rec ) {
                    console.log( rec )
                })
            })

openkvk returns the following error:

{ [Error: invalid response]
  error: [SyntaxError: Unexpected token <],
  code: 302,
  body: '<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset="UTF-8" />\n        <meta http-equiv="refresh" content="1;url=https://overheid.io/api/kvk/000ahold" />\n\n        <title>Redirecting to https://overheid.io/api/kvk/000ahold</title>\n    </head>\n    <body>\n        Redirecting to <a href="https://overheid.io/api/kvk/000ahold">https://overheid.io/api/kvk/000ahold</a>.\n    </body>\n</html>' }

What i expected is the following result:

// Koninklijke Ahold N.V. (Zaandam) // Ahold Group Support B.V. (Zaandam) // ... On the website https://overheid.io you can see that they receive the requests:

enter image description here

What am i doing wrong?

With kind regards Erik

fvdm commented 7 years ago

Ah yes I still need to port that package (and my blog post) to the Overheid.io API.

That might take a while, so you better use my overheid.io package instead, like this:

const kvk = require ('overheid.io') ({
  apikey: 'YOUR APIKEY',
  dataset: 'kvk'
});

function output (err, data) {
  console.dir (err || data, {
    depth: null,
    colors: true
  });
}

kvk ({
  params: {
    'filters[handelsnaam]': 'ahold'
  },
  callback: output
});
ErikHoeven commented 7 years ago

Thanks for your alternative solution,

I execute the following code (based on your readme.md) in the node_module\overheid.io folder.

var openkvk = require ('overheid.io') ({
    apikey: 'mijnAPI',
    dataset: 'kvk'
});

openkvk ({
                params: {
                    'filters[handelsnaam]': '*' + 'Advocaten' + '*'
                },
                callback: function (err, data) {
                    if (err) { return console.log (err) }

                    console.info(data.handelsnaam + ' : ' + data.plaats)
                }
              })

The result i get from the console is:

undefined : undefined

While i expected:

Advocaat 1  :  Plaats 1
Advocaat 2  :  Plaats 2

What am i doing wrong?

Manny thanks

fvdm commented 7 years ago
openkvk ({
  params: {
    query: '*Advocaten*',
    queryFields: ['handelsnaam'],
    fields: ['plaats']
  },
  callback: (err, data) => {
    if (err) { return console.log (err); }

    if (data.totalItemCount) {
      data._embedded.rechtspersoon.forEach (item => {
        console.log (item.handelsnaam + ' : ' + item.plaats);
      });
    }
  }
});

For some reason they include the country in plaats, I think that could be a bug in their API. I have filed a report with them.