export default function BarCode() {
const [hasPermission, setHasPermission] = useState(null);
const [scanned, setScanned] = useState(false);
const [data, setData] = useState();
const [value, setValue] = useState();
updateSearch = (data) => {
axios
.get(
`https://api.edamam.com/api/food-database/v2/parser?upc==${data}&app_id=2626c70d&app_key=0c0f87ae4e5437621363ecf8e7ea80ae&page=20`
)
.then((res) => {
setState({ value: console.log(res.data.hints) });
})
.catch((error) => {
console.log(error.response.value);
});
};
useEffect(() => {
(async () => {
const { status } = await BarCodeScanner.requestPermissionsAsync();
setHasPermission(status === "granted");
})();
}, []);
const handleBarCodeScanned = ({ type, data }) => {
setScanned(true);
setData(data);
};
if (hasPermission === null) {
return <Text>Requesting for camera permission</Text>;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View style={styles.container}>
<BarCodeScanner
onBarCodeScanned={scanned ? undefined : handleBarCodeScanned}
style={StyleSheet.absoluteFillObject}
>
{scanned && (
<Button
title={"Tap to Scan Again"}
onPress={() => setScanned(false)}
></Button>
)}
</BarCodeScanner>
</View>
);
}
Hey everyone, I'm trying to get food data from the Edamam database API through axios
, using a barcode scanner, at the moment when I run the code nothing shows on the console. And how can I put a If
statement so the if the food is not in the database a warning will show.
Link to BarCode documentation:https://docs.expo.io/versions/latest/sdk/bar-code-scanner/ Link to Edamam documentation:https://developer.edamam.com/food-database-api-docs