scrimba
Frontend Career Path
Advanced React
React Router
Create getVan function
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

AboutCommentsNotes
Create getVan function
Expand for more info
api.js
run
preview
console
import { initializeApp } from "firebase/app"
import { getFirestore, collection, getDocs } from "firebase/firestore/lite"

const firebaseConfig = {
apiKey: "AIzaSyD_k3v3HK3tKEqhlqFHPkwogW7PqEqhGhk",
authDomain: "vanlife-a1af5.firebaseapp.com",
projectId: "vanlife-a1af5",
storageBucket: "vanlife-a1af5.appspot.com",
messagingSenderId: "803007000356",
appId: "1:803007000356:web:446cd3a1ca406839258db1"
};

const app = initializeApp(firebaseConfig)
const db = getFirestore(app)

// Refactoring the fetching functions below
const vansCollectionRef = collection(db, "vans")

export async function getVans() {
const snapshot = await getDocs(vansCollectionRef)
const vans = snapshot.docs.map(doc => ({
...doc.data(),
id: doc.id
}))
return vans
}





// export async function getVans(id) {
// const url = id ? `/api/vans/${id}` : "/api/vans"
// const res = await fetch(url)
// if (!res.ok) {
// throw {
// message: "Failed to fetch vans",
// statusText: res.statusText,
// status: res.status
// }
// }
// const data = await res.json()
// return data.vans
// }

export async function getHostVans(id) {
const url = id ? `/api/host/vans/${id}` : "/api/host/vans"
const res = await fetch(url)
if (!res.ok) {
throw {
message: "Failed to fetch vans",
statusText: res.statusText,
status: res.status
}
}
const data = await res.json()
return data.vans
}

export async function loginUser(creds) {
const res = await fetch("/api/login",
{ method: "post", body: JSON.stringify(creds) }
)
const data = await res.json()

if (!res.ok) {
throw {
message: data.message,
statusText: res.statusText,
status: res.status
}
}

return data
}
Console
/vans/1
-3:53