Building A Photo Blog on Strapi Gatsby And Typescript: Part 2 Connecting the Backend to iNaturalist

Photo Content Type in Strapi
Photo Content-type in Strapi’s backend
Creating a photo entry with Shingleback lizard
Photo entry
const fetch = require("cross-fetch");module.exports = {
lifecycles: {
async beforeCreate(input) {
const res = await fetch(
`https://api.inaturalist.org/v1/observations/${input.iNatID}`
);
if (res.status >= 400) {
throw new Error("Bad response from iNaturalist observations server");
}
const d = await res.json();
const iNatResponse = d.results[0];
}
iNatData.taxonAncestors = iNatTaxonResponse.ancestors.map((ancestor) => {
return {
rank: ancestor.rank,
name: ancestor.name,
preferredCommonName: ancestor.preferred_common_name
? ancestor.preferred_common_name
: null,
};
});
Shingleback Lizard in Mungo, NSW
"use strict";
const fetch = require("cross-fetch");
/**
* Lifecycle callbacks for the `photo` model.
**/
module.exports = {
lifecycles: {
async beforeCreate(input) {
const res = await fetch(
`https://api.inaturalist.org/v1/observations/${input.iNatID}`
);
if (res.status >= 400) {
throw new Error("Bad response from iNaturalist observations server");
}
const d = await res.json();
const iNatResponse = d.results[0];
// hold all the iNatData in one json
let iNatData = {};
iNatData.commonName = iNatResponse.species_guess; //required
iNatData.placeGuess = iNatResponse.place_guess; // required
iNatData.dateTaken = iNatResponse.observed_on_details.date; // required
iNatData.latinName = iNatResponse.taxon.name; //required
iNatData.qualityGrade = iNatResponse.quality_grade;
iNatData.iNatDescription = iNatResponse.description;
iNatData.endemic = iNatResponse.taxon.endemic;
iNatData.threatened = iNatResponse.taxon.threatened;
iNatData.introduced = iNatResponse.taxon.introduced;
iNatData.native = iNatResponse.taxon.native;
// make a second call to iNat API to get taxon info
const taxonRes = await fetch(
`https://api.inaturalist.org/v1/taxa/${iNatResponse.taxon.id}`
);
if (taxonRes.status >= 400) {
throw new Error("Bad response from iNaturalist taxon server");
}
const x = await taxonRes.json();
const iNatTaxonResponse = x.results[0];
iNatData.taxonAncestors = iNatTaxonResponse.ancestors.map((ancestor) => {
return {
rank: ancestor.rank,
name: ancestor.name,
preferredCommonName: ancestor.preferred_common_name
? ancestor.preferred_common_name
: null,
};
});
// send it to strapi
input.iNatData = iNatData;
console.log("*************************");
console.log("input.iNatData");
console.log("*************************");
console.log(input.iNatData);
},
},
};

--

--

--

Developer. Musician. Naturalist. Traveler. In any order. @tayloredtotaylor

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Print String

Using Progressive Web Apps & React to build your mobile eCommerce

Testing React Function Components with Hooks using Enzyme

8 JavaScript debugging tools for 2018 and beyond

Simple test data generator in Angular

Making a generative recreation of A19 by László Moholy-Nagy in p5.js

Expo SDK 43 beta is now available

Using react-devtools with React Native

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Taylor Nodell

Taylor Nodell

Developer. Musician. Naturalist. Traveler. In any order. @tayloredtotaylor

More from Medium

How to implement authentication in NextJS using Amazon Cognito

Using Custom Controllers to Power a Next.js App

Exploration : Dashboard Application Using Metronic + React + Highcharts

Building a Nuxt.js static website with SEO meta tags