I made a writing function with Firebase, but when I search for the title, if it’s ice cream, it comes out when I search for ice cream, but it doesn’t come out when I search for cream I’ve tried a plug-in called searchflex, but it doesn’t work.
I asked the method I ordered on the Internet and AI, but I can’t do what I was told, so I have a question left.
import { db } from './firebaseConfig.js';
import { collection, getDocs } from 'firebase/firestore';
import { PostListManager } from './PostListManager.js';
export async function setupSearch() {
const searchButton = document.getElementById('searchBtn');
const searchInput = document.getElementById('searchInput');
const resultsContainer = document.getElementById('results');
const postListManager = new PostListManager('results');
async function loadData() {
try {
const querySnapshot = await getDocs(collection(db, 'posts'));
const posts = querySnapshot.docs.map(doc => ({
id: doc.id,
...doc.data()
}));
return posts;
} catch (error) {
console.error('Error loading data from Firestore:', error);
return [];
}
}
async function performSearch() {
const searchText = searchInput.value.trim().toLowerCase();
if (searchText === "") {
console.log("Empty search text.");
return;
}
try {
const posts = await loadData();
const filteredPosts = posts.filter(post => {
const titleMatch = post.title && post.title.toLowerCase().includes(searchText);
const descriptionMatch = post.description && post.description.toLowerCase().includes(searchText);
const contentMatch = post.content && post.content.toLowerCase().includes(searchText);
return titleMatch || descriptionMatch || contentMatch;
});
if (filteredPosts.length === 0) {
console.log('No matching documents found.');
} else {
console.log(`Found ${filteredPosts.length} matching documents.`);
}
postListManager.renderPosts(filteredPosts, false);
} catch (error) {
console.error('Error during search:', error);
}
}
// 검색 버튼 클릭 이벤트
searchButton.addEventListener('click', performSearch);
// 엔터 키 입력 시 검색 수행
searchInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
performSearch();
}
});
}