-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (33 loc) · 1.04 KB
/
index.js
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
36
37
38
39
40
41
import { initializeApp, cert } from "firebase-admin/app";
import{getFirestore} from "firebase-admin/firestore"
//import our credentials (serviceAccount)
import serviceAccount from './serviceAccount.js';
//connect to our firebase project using those credentials
initializeApp({
credential: cert(serviceAccount)
})
//connect to firestore database
const db = getFirestore();
//define a new video game:
const newGame = {
title: 'Frogger',
rated: 'E',
genre: 'Arcade',
realeased: 1981,
}
//create a doc inside a collection
db.collection('games').add(newGame)//go to database go to game collection adds data to game collection
//if ok, console.log the doc id (.then)
.then(doc => console.log('Game created:', doc.id))
//if not, console.log the error (.catch)
.catch(console.error)
//.catch(err=> console.error(err)) same as ^
//get all games
db.collection('games').get()
//reshape the collection
.then(collection => {
collection.docs.forEach(doc=> {
console.log(doc.id, doc.data())
})
})
.catch(console.error)