0

Attempting to use axios to make a get request at the following endpoint, and I keep getting errors:

When I check it using Postman and in a browser (GET request), it returns data just fine, but otherwise I can’t get a response.

This is the call I’m using, I don’t know if it’s some sort of issue with my code or with axios itself:

axios.get(`https://api.scryfall.com/cards/named?exact=${args.name}`)
            .then((res) => {
                console.log(JSON.stringify(res));
            })
            .catch((err) => {
                if (err.response) {
                    throw new Error(`Card with name (${name}) not found!`)
                }
                throw new Error(`Could not complete that query!`)
            }) 

The argument args.name is passed as part of a GraphQL resolver, and it definitely has a value, so not sure what the deal is.

Any help would be greatly appreciated!

MisutoWolf
  • 1,133
  • 1
  • 18
  • 33
  • What is the value of err.response? Why do you have ${args.name} in the call to get, but ${name} in the throw clause? What happens if you hardcode the name you are trying to match instead of using ${args.name}? – crosen9999 Jul 18 '20 at 23:55
  • Forgot to push the change to that first chunk of code, it’s `args.name` in both places currently. Hardcoding produced the same result last I checked but can double check after work. – MisutoWolf Jul 19 '20 at 00:39
  • Try hardcoding the following: "https://api.scryfall.com/cards/named?exact=anaba%20bodyguard". If that works, the issue is with the value of args.name. If it does not work, please console.log the value of JSON.stringify(err.response) and post what you get. – crosen9999 Jul 19 '20 at 00:50

1 Answers1

0

There are a couple of problems here.

Generally it's not a good idea to throw new errors after catching the original error. As written, your code throws an additional error because the axios Promise is thrown again instead of being dealt with inside catch - so node will complain that you didn't resolve or reject the promise.

The substantive issue is the same as the one answered here except for res - the actual error is TypeError: Converting circular structure to JSON which is caused by trying to JSON.stringify the res object:

JSON doesn't accept circular objects - objects which reference themselves. JSON.stringify() will throw an error if it comes across one of these.

The request (req) object is circular by nature - Node does that.

In this case, because you just need to log it to the console, you can use the console's native stringifying and avoid using JSON

So you can fix this by changing your code to:

axios.get(`https://api.scryfall.com/cards/named?exact=${args.name}`)
          .then((res) => {
              console.log(res)
          })
          .catch((err) => {
            console.log(err)
              if (err.response) {
                  console.error(`Card with name (${name}) not found!`)
              } else {
                console.error(`Could not complete that query!`)
              }
          })

If you were just using console.log for testing/as an example and actually need to stringify the data to use some other way, just make sure you're stringifying the data (which is presumably what you actually want) and not the whole res object:

axios.get(`https://api.scryfall.com/cards/named?exact=${args.name}`)
          .then((res) => {
              let scryfallData = JSON.stringify(res.data)
              doSomethingWith(scryfallData)
          })
Hugh
  • 371
  • 1
  • 8
  • The code as is works perfectly fine on my machine when replacing args.name with a proper value, and also the post you reference applies to request and not result, so your explanation does not seem to make sense – crosen9999 Jul 19 '20 at 04:53
  • I don't see how that's possible if you are running it with nodejs rather than in a browser - are you calling `JSON.stringify(res)` or `JSON.stringify(res.data)` ? – Hugh Jul 19 '20 at 05:00
  • Hmm, I ran it with res.data, which is strange because I had copy/pasted the OP. My apologies, because your response is correct based on the post. – crosen9999 Jul 19 '20 at 05:15