, пожалуйста, помогите мне и скажите, правильно ли это?
Код: Выделить всё
const mongoose = require("mongoose");
const seedAdminUser = require("./seed.Admin");
dotenv.config();
const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("Connected to MongoDB");
await seedAdminUser();
} catch (err) {
console.error(" MongoDB connection error:", err.message);
process.exit(1);
}
};
module.exports = connectDB;
Код: Выделить всё
const bcrypt = require("bcryptjs");
const Admin = require("../models/admin.model");
const seedAdminUser = async () => {
try {
const adminEmail = process.env.ADMIN_EMAIL;
const adminPassword = process.env.ADMIN_PASSWORD;
// Check if admin exists
const admin = await Admin.findOne({ email: adminEmail });
if (admin) {
console.log("Admin already exists");
//Reactivate admin if disabled
if (!admin.isActive) {
admin.isActive = true;
await admin.save();
console.log("Admin reactivated!");
}
return;
}
// Hash Password
const hashedPassword = await bcrypt.hash(adminPassword, 10);
// Create Admin Admin
await Admin.create({
name: "Admin",
email: adminEmail,
password: hashedPassword,
isActive: true,
});
console.log(" Admin user seeded successfully");
} catch (err) {
console.error(" Error seeding admin user:", err.message);
}
};
module.exports = seedAdminUser;
Подробнее здесь: https://stackoverflow.com/questions/794 ... in-node-js
Мобильная версия