/
index.js
45 lines (37 loc) · 960 Bytes
/
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
42
43
44
const firebase = require("firebase");
const config = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: ""
};
firebase.initializeApp(config);
const db = firebase.firestore();
async function execute() {
const userData = {
name: "name" + (new Date()).getTime(),
};
const userRef = await db.collection("users").add(userData);
const postData = {
name: "post" + (new Date()).getTime(),
user: userRef
};
const postRef = await db.collection("posts").add(postData);
const querySnapshot = await db.collection("posts").get();
const posts = [];
querySnapshot.forEach(doc => {
posts.push(doc.data());
});
for (let i = 0; i < posts.length; i++) {
const post = posts[i];
const userQuerySnapshot = await post.user.get();
post.userData = userQuerySnapshot.data();
}
return posts;
}
execute().then(posts => {
console.log(posts);
process.exit(0);
});