I want to get the data from my database using JavaScript and firebase, I have configured the firebase connection, but it gives me error: db is not defined
.
I think the problem comes from the database configuration with firebase. because the tutorial configuration is different than that. I found this configuration in a post : previous solution
db.collection('courses').get()
.then(res => console.log(res))
.catch(err => console.error(err))
<!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>Document</title>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
</head>
<body>
<div class="container my-5">
<div class="row">
<div class="col-4 mx-auto">
<h3 class="my-3 text-center"><span class="badge badge-info">MARZOUK</span></h3>
<form>
<div class="input">
<div class="input-group">
<input type="text" class="form-control" id="course" placeholder="add new course">
<div class="input-group-apprend">
<input type="submit" class="btn btn-outline-secondary">
</div>
</div>
</div>
</form>
</div>
</div>
<ul class="list-group mt-2">
<li class="list-group-item">element</li>
<li class="list-group-item">element</li>
<li class="list-group-item">element</li>
</ul>
</div>
<script type="module">
import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.8.3/firebase-app.js';
import { getFirestore, collection, getDocs } from 'https://www.gstatic.com/firebasejs/9.8.3/firebase-firestore-lite.js';
const firebaseConfig = {
apiKey: "Myapikey",
authDomain: "javascript-3bbaa.firebaseapp.com",
projectId: "javascript-3bbaa",
storageBucket: "javascript-3bbaa.appspot.com",
messagingSenderId: "<myId>",
appId: "<MyappId>"
};
// Initialize Firebase
const firebase = initializeApp(firebaseConfig);
const db = getFirestore(firebase);
</script>
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2" crossorigin="anonymous"></script>
<script src="script.js"></script>
</body>
</html>