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

asked on

How can I get these two "GETs" to play nicely?

I'm building a Fitbit API and we're going great guns but I've run into a snag and I'm confident it's due to my being new to Node.js.

This works:

exports.getCallback = (req, res, next) => {
	client.getAccessToken(req.query.code, 'http://localhost:3000/callback')
	.then(result => { 
	// use the access token to fetch the user's profile information
	//client.get("/profile.json", result.access_token)
	client.get('/activities/date/2019-11-07.json', result.access_token)
		.then(results => {
			console.log(results[0].goals.calories);
			//res.send(results[0]);
		}).catch(err => {
			const error = new Error(err);
			error.httpStatusCode = 500;
			return next(error);
		});
		
	});
};

Open in new window


...and this works:

exports.getCallback = (req, res, next) => {
	client.getAccessToken(req.query.code, 'http://localhost:3000/callback')
	.then(result => { 
	// use the access token to fetch the user's profile information
	//client.get("/profile.json", result.access_token)
		client.get("/activities/calories/date/today/1d.json", result.access_token)
		.then(answers => {
			.then(answers => {
			res.send(answers[0]["activities-calories"][0].value);
		}).catch(err => {
			const error = new Error(err);
			error.httpStatusCode = 500;
			return next(error);
		});
	});
};

Open in new window


Now, are you ready for this?

I've got to retrieve information that's coming from multiple endpoints. So, I tried to do this:

exports.getCallback = (req, res, next) => {
	client.getAccessToken(req.query.code, 'http://localhost:3000/callback')
	.then(result => { 
	// use the access token to fetch the user's profile information
	//client.get("/profile.json", result.access_token)
	client.get('/activities/date/2019-11-07.json', result.access_token)
		.then(results => {
			console.log(results[0].goals.calories);
			//res.send(results[0]);
		}).catch(err => {
			const error = new Error(err);
			error.httpStatusCode = 500;
			return next(error);
		});
		[b]client.get("/activities/calories/date/today/1d.json", result.access_token)
		.then(answers => {
			.then(answers => {
			res.send(answers[0]["activities-calories"][0].value);
		}).catch(err => {
			const error = new Error(err);
			error.httpStatusCode = 500;
			return next(error);
		});[/b]
	});
};

Open in new window


And I get this error:

         .then(answers => {
                        ^

SyntaxError: Unexpected token .

I'm assuming it's because I'm going about this the wrong way.

I need two sets of information, one from "client.get(activities)' and one from "client.get('activites/calories')."

How do I make that happen?
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland 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

Chris!

This will do the trick! I wish you were local, I'd buy you a Chick Fil A Gift Card!
Haha. I have no idea what that is, but thanks (I think!)