Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

How do I take what I'm receiving as a GET and automatically POST it into a database?

I'm working with a fully functional Node.js code that interacts with the Fitbit API.

Being somewhat new to Node, however, has me in a spot where I need a little guidance.

The code looks like this:

// initialize the express application
const express = require("express");
const app = express();

// initialize the Fitbit API client
const FitbitApiClient = require("fitbit-node");
const client = new FitbitApiClient({
	clientId: "CLIENT_ID", //client_id
	clientSecret: "CLIENT_SECRET", //client secret
	apiVersion: '1.2' // 1.2 is the default
});

// redirect the user to the Fitbit authorization page
app.get("/authorize", (req, res) => {
	// request access to the user's activity, heartrate, location, nutrion, profile, settings, sleep, social, and weight scopes
	res.redirect(client.getAuthorizeUrl('activity heartrate location nutrition profile settings sleep social weight', 'http://localhost:3000/callback')); //callback url
});

// handle the callback from the Fitbit authorization flow
app.get("/callback", (req, res) => {
	// exchange the authorization code we just received for an access token
	client.getAccessToken(req.query.code, 'http://localhost:3000/callback').then(result => { //callback url
		// use the access token to fetch the user's profile information
		[b]client.get("/profile.json", result.access_token)[/b]
		[u]//client.get ("/1/user/[user-id]/[resource-path]/date/[date]/[period].json", result.access_token)[/u]
		.then(results => {
			res.send(results[0]);
			console.log(results[0].user.age);
		}).catch(err => {
			res.status(err.status).send(err);
		});
	}).catch(err => {
		res.status(err.status).send(err);
	});
});

// launch the server
app.listen(3000);

Open in new window


Notice what's in bold. The GET "profile.json" file is arriving as a block of JSON values but here's where I'm stuck.

I need to take that JSON file and insert the content into a database. Generally speaking that's going to be done in the context of a POST or PUSH. How to I take that info that I'm receiving as a GET profile.json and route it in a way where it's being thrown into a database automatically?

Silly question, perhaps, but there it is.

Thanks!

Here's the JSON:

{ 
   "user":{ 
      "age":22,
      "ambassador":false,
      "autoStrideEnabled":true,
      "avatar":"https://static0.fitbit.com/images/profile/defaultProfile_100.png",
      "avatar150":"https://static0.fitbit.com/images/profile/defaultProfile_150.png",
      "avatar640":"https://static0.fitbit.com/images/profile/defaultProfile_640.png",
      "averageDailySteps":5296,
      "clockTimeDisplayFormat":"12hour",
      "corporate":false,
      "corporateAdmin":false,
      "dateOfBirth":"1983-08-26",
      "displayName":"Bruce G.",
      "displayNameSetting":"name",
      "distanceUnit":"en_US",
      "encodedId":"7V335V",
      "familyGuidanceEnabled":false,
      "features":{ 
         "exerciseGoal":true
      },
      "firstName":"Bruce",
      "foodsLocale":"en_US",
      "fullName":"Bruce Gust",
      "gender":"MALE",
      "glucoseUnit":"en_US",
      "height":175.20000000000002,
      "heightUnit":"en_US",
      "isChild":false,
      "isCoach":false,
      "lastName":"Gust",
      "locale":"en_US",
      "memberSince":"2019-10-22",
      "mfaEnabled":false,
      "offsetFromUTCMillis":-21600000,
      "startDayOfWeek":"SUNDAY",
      "strideLengthRunning":95.60000000000001,
      "strideLengthRunningType":"default",
      "strideLengthWalking":72.7,
      "strideLengthWalkingType":"default",
      "swimUnit":"en_US",
      "timezone":"America/Chicago",
      "topBadges":[ 
         { 
            "badgeGradientEndColor":"00D3D6",
            "badgeGradientStartColor":"007273",
            "badgeType":"DAILY_STEPS",
            "category":"Daily Steps",
            "cheers":[ 

            ],
            "dateTime":"2019-11-04",
            "description":"15,000 steps in a day",
            "earnedMessage":"Congrats on earning your first Urban Boot badge!",
            "encodedId":"228TMK",
            "image100px":"https://static0.fitbit.com/images/badges_new/100px/badge_daily_steps15k.png",
            "image125px":"https://static0.fitbit.com/images/badges_new/125px/badge_daily_steps15k.png",
            "image300px":"https://static0.fitbit.com/images/badges_new/300px/badge_daily_steps15k.png",
            "image50px":"https://static0.fitbit.com/images/badges_new/badge_daily_steps15k.png",
            "image75px":"https://static0.fitbit.com/images/badges_new/75px/badge_daily_steps15k.png",
            "marketingDescription":"You've walked 15,000 steps  And earned the Urban Boot badge!",
            "mobileDescription":"With a number that's almost three times more than the national average, your step count is really heating up.",
            "name":"Urban Boot (15,000 steps in a day)",
            "shareImage640px":"https://static0.fitbit.com/images/badges_new/386px/shareLocalized/en_US/badge_daily_steps15k.png",
            "shareText":"I took 15,000 steps and earned the Urban Boot badge! #Fitbit",
            "shortDescription":"15,000 steps",
            "shortName":"Urban Boot",
            "timesAchieved":1,
            "value":15000
         },
         { 
            "badgeGradientEndColor":"38D7FF",
            "badgeGradientStartColor":"2DB4D7",
            "badgeType":"DAILY_FLOORS",
            "category":"Daily Climb",
            "cheers":[ 

            ],
            "dateTime":"2019-11-04",
            "description":"50 floors in a day",
            "earnedMessage":"Congrats on earning your first Lighthouse badge!",
            "encodedId":"228TT7",
            "image100px":"https://static0.fitbit.com/images/badges_new/100px/badge_daily_floors50.png",
            "image125px":"https://static0.fitbit.com/images/badges_new/125px/badge_daily_floors50.png",
            "image300px":"https://static0.fitbit.com/images/badges_new/300px/badge_daily_floors50.png",
            "image50px":"https://static0.fitbit.com/images/badges_new/badge_daily_floors50.png",
            "image75px":"https://static0.fitbit.com/images/badges_new/75px/badge_daily_floors50.png",
            "marketingDescription":"You've climbed 50 floors to earn the Lighthouse badge!",
            "mobileDescription":"With a floor count this high, you're a beacon of inspiration to us all!",
            "name":"Lighthouse (50 floors in a day)",
            "shareImage640px":"https://static0.fitbit.com/images/badges_new/386px/shareLocalized/en_US/badge_daily_floors50.png",
            "shareText":"I climbed 50 flights of stairs and earned the Lighthouse badge! #Fitbit",
            "shortDescription":"50 floors",
            "shortName":"Lighthouse",
            "timesAchieved":1,
            "value":50
         }
      ],
      "waterUnit":"en_US",
      "waterUnitName":"fl oz",
      "weight":81.6,
      "weightUnit":"en_US"
   }
}

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

what database?
Avatar of noci
noci

You post stuff to webservers.   So you most probably need some webserver address and URI  (aka URL) to the API you intend to use.

The result may very well be that the data is put into a database.
(mostly the nomenclature around database talks about inserting records(tuples) into a database(table).
GET / POST / (and a few more)  are from the HTTP dictionary)
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER