We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 03fffbd commit a41e7f9Copy full SHA for a41e7f9
0x05-Node_JS_basic/full_server/utils.js
@@ -0,0 +1,27 @@
1
+// file containing function to read from database
2
+const { readFile } = require('fs');
3
+
4
+module.exports = function readDatabase(filePath) {
5
+ const students = {};
6
+ return new Promise((resolve, reject) => {
7
+ readFile(filePath, (err, data) => {
8
+ if (err) {
9
+ reject(err);
10
+ } else {
11
+ const lines = data.toString().split('\n');
12
+ const noHeader = lines.slice(1);
13
+ for (let i = 0; i < noHeader.length; i += 1) {
14
+ if (noHeader[i]) {
15
+ const field = noHeader[i].toString().split(',');
16
+ if (Object.prototype.hasOwnProperty.call(students, field[3])) {
17
+ students[field[3]].push(field[0]);
18
19
+ students[field[3]] = [field[0]];
20
+ }
21
22
23
+ resolve(students);
24
25
+ });
26
27
+};
0 commit comments