petfinder-com / petfinder-js-sdk

Petfinder.com API client for JavaScript/TypeScript
https://www.petfinder.com/developers/
BSD 3-Clause "New" or "Revised" License
59 stars 18 forks source link

Trying to update the API #14

Closed Slaestat closed 5 years ago

Slaestat commented 5 years ago

Hi, I'm trying to implement the new version, and in an empty page the following code works for me:

var pf = new petfinder.Client({apiKey: "my-api-key", secret: "my-api-secret"});

pf.animal.search() .then(function (response) { // Do something with response.data.animals }) .catch(function (error) { // Handle the error });

However, on full site, new petfin.... doesn't give me the token (pf.config.token). Is there anything that might be preventing the API from giving me the token ?

My apologize for the inconvenient.

mloberg commented 5 years ago

It may have to due with the requests being async. You may be trying to access that value before the request has completed.

If you used await does that change your result?

var pf = new petfinder.Client(...);

var animals = await pf.animal.search();
Slaestat commented 5 years ago

Uncaught SyntaxError: await is only valid in async function

I got this error.

mloberg commented 5 years ago

Wrap it in an async function

(async function () {
    // code here
})();
Slaestat commented 5 years ago

As an update to this issue I don't know why, but it is now working (without the await or something like that). Thanks for your assistance.