-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
50 lines (28 loc) · 860 Bytes
/
test.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
42
43
44
45
46
47
48
49
50
const express = require('express');
const { initializeApp, getApps, cert } = require('firebase-admin/app');
const { getFirestore } = require('firebase-admin/firestore');
const credentials = require('./credentials.json');
const app = express();
const PORT = process.env.PORT || 3000;
function connectToFirestore() {
if(!getApps().length) {
initializeApp({
credential: cert(credentials)
});
}
return getFirestore();
}
app.get('/customers', async (req, res) => {
const db = connectToFirestore();
db.collection('customers').get()
.then(snapshot => {
const customers = snapshot.docs.map(doc => {
customer.id = doc.id;
return customer
});
res.status(200).send(customers);
})
.catch(console.error);
})
app.listen(PORT, () => console.log(`Listening on port ${PORT}...`));
};