Created
March 23, 2021 19:18
-
-
Save Longwater1234/6bbcf2ae2bd1840562146910b008b502 to your computer and use it in GitHub Desktop.
Back4App Parse Calories Cloud Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require("axios").default; | |
//CLOUD function for getting calories for given `foodName` | |
Parse.Cloud.define("getcalories", async(request) => { | |
const foodName = request.params.foodName; | |
const options = { | |
method: "GET", | |
url: "https://api.calorieninjas.com/v1/nutrition", | |
params: { query: foodName }, | |
headers: { | |
"X-Api-Key": "YOUR-API-KEY-HERE-SIGNUP::CALORIENINJAS.COM" | |
} | |
}; | |
return axios.request(options).then((response) => { | |
var resultObject = response.data.items[0]; | |
if (resultObject == null) { | |
console.error("No food info found!"); | |
return "0"; | |
} else { | |
console.log(resultObject); | |
return resultObject.calories.toString(); | |
} | |
}).catch((error) => { | |
console.error(error.message); | |
console.error(error.response.data); | |
return "0"; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment