-
Notifications
You must be signed in to change notification settings - Fork 1
/
first.js
67 lines (50 loc) · 2.01 KB
/
first.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const express = require('express');
const mongoose = require('mongoose');
const multer = require('multer');
const xlsx = require('xlsx');
const cors = require('cors');
const app = express();
const PORT = 3001;
app.use(cors());
mongoose.connect('mongodb://localhost:27017/Student', { useNewUrlParser: true, useUnifiedTopology: true });
const timetableSchema = new mongoose.Schema({
username: String,
password: String,
});
const Timetable = mongoose.model('details', timetableSchema);
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
app.post('/upload', upload.single('file'), async (req, res) => {
try {
const workbook = xlsx.read(req.file.buffer, { type: 'buffer' });
const sheetName = workbook.SheetNames[0];
const excelData = xlsx.utils.sheet_to_json(workbook.Sheets[sheetName]);
console.log('Excel Data:', excelData); // Log the Excel data for debugging
const timetableData = [];
for (const entry of excelData) {
const username = entry['username'];
const password = entry['password'];
// Use findOne to check if the user already exists in the database
const existingUser = await Timetable.findOne({ username: username });
if (username && password && !existingUser) {
console.log(`Condition is true for: ${username}, ${password}`);
const timetableEntry = new Timetable({
username: username,
password: password,
});
timetableData.push(timetableEntry);
} else {
console.log(`User with username ${username} already exists or missing data.`);
}
}
await Timetable.insertMany(timetableData);
console.log("Data successfully inserted");
res.status(200).json({ message: 'Timetable data inserted successfully!' });
} catch (error) {
console.error('Error processing file:', error);
res.status(500).json({ message: 'Error processing file', error: error.message });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});