Understanding what typeof thing you're dealing with
Information Flaneur 2023-05-21
Summary:
Yesterday I was supposed to be working on a demo for tinfoil, but I got distracted looking at the New York Public Library digital collections API.
I ended up making ...yes, another Twitter bot. It's pretty simple - you tweet a word or phrase at it, and it does its best to send you back a picture from the NYPL collection that matches what you tweeted.
Mostly it uses things I've had experience with before - packages random-js and twit, and the node file system 'fs'. There were a few new things I tried though - saving and retrieving image files using fs, and tweeting images.
There were also a couple of bugs I created by not understanding what 'types' I was using. If you're interested in learning nodejs and JavaScript, this post might help save you a lot of frustration!
The first problem I came up against was that recently heard about the npm package chalk and wanted to play around with it. Chalk allows you to selectively add colours in the console. It's useful if you want to differentiate between different outputs (e.g. you might make error messages red). What I've learned as a result is useful, but it was pretty painful.
When first using an API, I find I always need to look hard at the JSON object it sends back, because the API documentation often makes perfect sense once you understand the data structure, and just as often makes little sense before you understand the data structure! To see what the various parts were, I simply requested some data (i.e. sent a query) from the API, then logged the data to the console:
console.log(data)
This resulted in a huge block of text, which was pretty difficult to read, but it was easy enough to find that the first 'key' was, appropriately enough, nyplAPI. So, knowing my JSON dot notation I confidently changed my instruction to:
console.log(data.nyplAPI)
...and was rewarded with undefined. What the hell?
Sometimes strings look like objects
The problem here is that I was thinking that the data returned by the API was an object. It certainly looked like one. But it was actually a string - part of the reason it was so hard to read, coming through as a big block without spacing. I needed to turn it back into an object to make it usable, using JSON.parse():
var parsed = JSON.parse(body);
Now I could use console.log(parsed.nyplAPI) and get a result:
{ nyplAPI:
{ request:
{ search_text: 'fun',
filter_query: 'use_rtxt_s:("PDCDPP" OR "PDNCN" OR "PDREN" OR "PDEXP" OR "PDADD" OR "PDUSG" OR "PPD" OR "PPD100" OR "CC_0")',
filter_type: 'fuzzy',
perPage: '50',
page: '1',
totalPages: '4',
startTime: 'Beginning of Time',
endTime: 'Till Now' },
response: { headers: [Object], numResults: '152', result: [Object] } } }
Great! Now I can use chalk to make it a pretty colour:
console.log(chalk.yellow(parsed.nyplAPI));
Nope. Instead of the same result with yellow text, the console logged [object Object]. What on earth was going on here? At the time, I simply gave up on using chalk, but it turns out the answer is both simple and complex. I could easily have resolved my problem by turning it back into a string using