-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
35 lines (30 loc) · 886 Bytes
/
Copy pathdb.js
File metadata and controls
35 lines (30 loc) · 886 Bytes
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
const mongoose = require('mongoose');
const User = require('./models/User'); // Import the User model
// Connection URI
const dbURI = 'mongodb://localhost:27017/finflare'; // Adjust if needed
// Connect to MongoDB
mongoose.connect(dbURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log('Connected to MongoDB');
// Create a new user
const createUser = async () => {
const newUser = new User({
name: 'John Doe',
email: 'johndoe@example.com',
age: 30,
});
try {
const savedUser = await newUser.save();
console.log('User saved:', savedUser);
} catch (err) {
console.error('Error saving user:', err);
}
};
createUser(); // Call the function to create a user
})
.catch((err) => {
console.log('Error connecting to MongoDB:', err);
});