Pārlūkot izejas kodu

fix: 错误捕捉

IlhamTahir 1 gadu atpakaļ
vecāks
revīzija
4742e2013d
1 mainītis faili ar 20 papildinājumiem un 16 dzēšanām
  1. 20 16
      src/core/service/user.service.ts

+ 20 - 16
src/core/service/user.service.ts

@@ -26,24 +26,28 @@ export class UserService {
   }
 
   async createInitialUser() {
-    const existingUser = await this.userRepository.findOne({
-      where: { username: 'admin' },
-    });
-    if (!existingUser) {
-      const salt = await bcrypt.genSalt();
-      const encryptedPassword = await bcrypt.hash('admin123', salt);
-
-      const user = this.userRepository.create({
-        username: 'admin',
-        encryptedPassword,
-        locked: false,
-        enabled: true,
+    try {
+      const existingUser = await this.userRepository.findOne({
+        where: { username: 'admin' },
       });
+      if (!existingUser) {
+        const salt = await bcrypt.genSalt();
+        const encryptedPassword = await bcrypt.hash('admin123', salt);
+
+        const user = this.userRepository.create({
+          username: 'admin',
+          encryptedPassword,
+          locked: false,
+          enabled: true,
+        });
 
-      await this.userRepository.save(user);
-      console.log('Initial admin user created');
-    } else {
-      console.log('Admin user already exists');
+        await this.userRepository.save(user);
+        console.log('Initial admin user created');
+      } else {
+        console.log('Admin user already exists');
+      }
+    } catch (error) {
+      console.log('Error creating initial user', error);
     }
   }
 }