mirror of
https://github.com/FranP-code/Hangman-game-with-React.git
synced 2025-10-13 00:42:32 +00:00
Added the firebase integration
This commit is contained in:
29
src/Firebase Querys/GetRandomCategory.js
Normal file
29
src/Firebase Querys/GetRandomCategory.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import {firestore} from '../Firebase/Firebase_Config'
|
||||
import { getFirestore, collection, doc, getDocs, getDoc } from 'firebase/firestore/lite';
|
||||
|
||||
const GetRandomCategory = async () => {
|
||||
|
||||
|
||||
let categoriesList = []
|
||||
|
||||
try {
|
||||
|
||||
const db = getFirestore(firestore)
|
||||
const data = collection(db, `categories`)
|
||||
const result = await getDocs(data)
|
||||
|
||||
result.docs.map(doc => categoriesList.push(doc.id.toLowerCase()))
|
||||
|
||||
const randomNumber = Math.trunc(
|
||||
|
||||
Math.random() * (categoriesList.length - 0) + 0
|
||||
)
|
||||
|
||||
return categoriesList[randomNumber]
|
||||
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
}
|
||||
|
||||
export default GetRandomCategory
|
||||
Reference in New Issue
Block a user