-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_note.js
More file actions
70 lines (63 loc) · 2.45 KB
/
Copy pathupload_note.js
File metadata and controls
70 lines (63 loc) · 2.45 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const https = require('https');
// Your Firebase project ID
const projectId = "skill-notes";
const databaseId = "notes"; // the named database
// Note data to upload
const noteData = {
title: { stringValue: "Advanced Java Programming" },
subjectName: { stringValue: "Advanced Java Programming" },
subjectId: { stringValue: "Advanced Java Programming" },
subject: { stringValue: "Advanced Java Programming" },
unit: { stringValue: "UNIT- 1" },
collegeName: { stringValue: "Medi-Caps University" },
collegeId: { stringValue: "medicaps" },
college: { stringValue: "medicaps" },
branch: { stringValue: "cse" },
semester: { stringValue: "Semester 4" },
year: { stringValue: "2nd Year" },
url: { stringValue: "https://drive.google.com/file/d/1oWspWtT2VdTwdmQCxrnmgShSZ2cW-ze2/view" },
fileUrl: { stringValue: "https://drive.google.com/file/d/1oWspWtT2VdTwdmQCxrnmgShSZ2cW-ze2/view" },
driveLink: { stringValue: "https://drive.google.com/file/d/1oWspWtT2VdTwdmQCxrnmgShSZ2cW-ze2/view" },
uploader: { stringValue: "Tanishq Agrawal" },
uploaderName: { stringValue: "Tanishq Agrawal" },
status: { stringValue: "approved" },
type: { stringValue: "notes" },
likes: { integerValue: "0" },
views: { integerValue: "0" },
downloads: { integerValue: "0" },
dislikes: { integerValue: "0" }
};
const payload = JSON.stringify({ fields: noteData });
const options = {
hostname: 'firestore.googleapis.com',
port: 443,
path: `/v1/projects/${projectId}/databases/${databaseId}/documents/notes`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(payload)
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => data += chunk);
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
console.log('✅ Successfully created document in Firestore!');
try {
const json = JSON.parse(data);
console.log('Document ID:', json.name.split('/').pop());
} catch (e) {
console.log('Response:', data);
}
} else {
console.error('❌ Failed to create document. Status:', res.statusCode);
console.error('Response:', data);
}
});
});
req.on('error', (e) => {
console.error(`Problem with request: ${e.message}`);
});
req.write(payload);
req.end();