kinotto / geonames.js

Nodejs and Browser client for Geonames.org REST API :earth_africa:
http://www.geonames.org/export/client-libraries.html
MIT License
88 stars 17 forks source link

Cannot read property 'create' of undefined #6

Closed Zeni241 closed 6 years ago

Zeni241 commented 6 years ago

HI I am trying to use geonames.js in browser. I am on local machine and address in browser is http://localhost:57745/index.html Following is my code.

<script type="text/javascript" src="geonames.min.js"></script>

<button type="button" class="btn btn-primary" onclick="getCities()">getCities</button>

async function getCities () {
            const geonames = new Geonames({ username: 'myusername', lan: 'en', encoding: 'JSON' });
            try {
                const countries = await geonames.countryInfo({}) //get continents
                const states = await geonames.children({ geonameId: countries.geonames[0].geonameId })
                const regions = await geonames.children({ geonameId: states.geonames[0].geonameId });
                const cities = await geonames.children({ geonameId: regions.geonames[0].geonameId });
                console.log(cities.geonames);
                $("#geo").text(cities.geonames);

            } catch (err) {
                console.error(err);
            }

        };

I am getting this error

geonames.min.js:1 Uncaught (in promise) TypeError: Cannot read property 'create' of undefined
    at new c (geonames.min.js:1)
    at getCities (index.html:357)
    at HTMLButtonElement.onclick (index.html:141)
c @ geonames.min.js:1
getCities @ index.html:357
onclick @ index.html:141

This is my first time using this library. Any help please. Zeni

kinotto commented 6 years ago

hi @Zeni241 and thanks for using the library,

const geonames = new Geonames({ username: 'myusername', lan: 'en', encoding: 'JSON' });

did you actually register to http://www.geonames.org/login ? cause the username must be a real one and not the placeholder "myusername" that you are using now.

Thanks

Zeni241 commented 6 years ago

Thanks for reply. Yes I did register and I can login to your site with this user name.

kinotto commented 6 years ago

Sorry, just to understand exactly what's going on, did you add your username in the config object?

const geonames = new Geonames({ username: 'YOUR USERNAME', lan: 'en', encoding: 'JSON' });

Another thing to note, it's not clear how did you install the module itself, try to install it as a npm module.

npm install --save geonames.js

and then import it depending on your environment with:

  import Geonames from 'geonames.js'; /* es module */
  const Geonames = require('geonames.js'); /* umd */

Thanks