-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
35 lines (30 loc) · 1.46 KB
/
Copy pathapp.js
File metadata and controls
35 lines (30 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var database = firebase.database();
function searchAuthorByName() {
var authorName = document.getElementById("authorSearch").value;
var authorsRef = database.ref("authors");
var resultsDiv = document.getElementById("searchResults");
authorsRef.orderByChild("name").equalTo(authorName).once("value")
.then((snapshot) => {
if (snapshot.exists()) {
snapshot.forEach(function (childSnapshot) {
var authorData = childSnapshot.val();
var authorBooks = authorData.books;
resultsDiv.innerHTML = ""; // Clear previous results
resultsDiv.innerHTML = `Books by ${authorName}:`;
console.log(`Books by ${authorName}:`);
for (var bookId in authorBooks) {
var book = authorBooks[bookId];
resultsDiv.innerHTML += "<p><strong>" + book.title + ":</strong> " + book.publishedYear + "</p>";
console.log(`- ${book.title} (${book.publishedYear})`);
}
document.getElementById("authorSearch").value = "";
});
} else {
resultsDiv.innerHTML = "No results found for " + authorName;
console.log(`Author ${authorName} not found.`);
}
})
.catch(function(error) {
console.error("Error searching for author:", error);
});
}