hey guys im a beginner and am trying to
hey guys im a beginner and am trying to build an animal fact app for my kids
im trying to use D1 database to store the animal names, facts, image url (to R3 bucket public link)
my issue is that I can't for the life of me figure out how to interact with the D1 database in my page or html/js
https://javascript-website.pages.dev/
it's still a while away from something semi decent, i figured it out with python locally but now im trying to turn it into JS and deploy it online with cloudflare because of the D1 integration it has. Thanks in advance!
4 Replies
thanks!
would it be
const response = await fetch("/functions/getAnimals", { as its in the functions directory?
const response = await fetch("/functions/getAnimals", { as its in the functions directory?
ok thanks, I pasted the code in index.html :
and received an error
(index):79 Uncaught (in promise) Error: Response was not ok.
at (index):79:23
``<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ANIMAL FACTS! 🐻</title>
<link rel="stylesheet" href="stylesheet.css">
<script src="javascript.js" defer></script>
<script src="functions/getAnimals.js" defer></script>
<script src="functions/dump.js" defer></script>
<script src="functions/helloworld.js" defer></script>
</head>
<body>
some code
<script>
some code
(async () => {
const response = await fetch("/getAnimals", {
method: "GET",
headers: {
"Accept": "application/json"
}
});
if (!response.ok) {
throw new Error("Response was not ok.");
}
const result = await response.json();
// you can now use result as
data`
console.log(result);
})();
</script>
</body>
</html>!! it worked!
My mistake was in my getAnimals.js file
the SQL query was incorrect - I didn't change 'users' to 'animals'
thanks for all your help