ryoji-s 2 лет назад
Родитель
Сommit
79cfb1b73c
1 измененных файлов с 9 добавлено и 1 удалено
  1. 9 1
      apps/app/src/server/service/page.ts

+ 9 - 1
apps/app/src/server/service/page.ts

@@ -195,6 +195,9 @@ class PageService {
   async isUsersHomepageOwnerAbsent(path: string): Promise<boolean> {
     const User = mongoose.model('User');
     const username = getUsernameByPath(path);
+    if (username == null) {
+      throw new Error('Cannot found username by path');
+    }
     const ownerExists = await User.exists({ username });
     return ownerExists === null;
   }
@@ -237,11 +240,16 @@ class PageService {
     }
 
     const User = mongoose.model('User');
-    const usernames = userHomepages.map(page => getUsernameByPath(page.path)) as string[];
+    const usernames = userHomepages
+      .map(page => getUsernameByPath(page.path))
+      .filter(username => username !== null) as string[];
     const existingUsernames = await User.distinct<string>('username', { username: { $in: usernames } });
 
     const isUserHomepageDeletable = (page: PageDocument) => {
       const username = getUsernameByPath(page.path);
+      if (username == null) {
+        throw new Error('Cannot found username by path');
+      }
       return !existingUsernames.includes(username);
     };