@@ -2,20 +2,22 @@ import { HookContext } from '@feathersjs/feathers';
2
2
import bluebird from 'bluebird' ;
3
3
import _ from 'lodash' ;
4
4
5
- import { PollSchema } from '../../models/polls/poll.schema' ;
6
- import { UserSchema } from '../../models/users/user.schema' ;
5
+ import { Poll , PollSchema } from '../../models/polls/poll.schema' ;
6
+ import { User } from '../../models/users/user.schema' ;
7
7
import UserModel from '../../models/users/user.model' ;
8
8
9
9
10
- interface Poll extends Omit < PollSchema , 'authorId' > {
11
- author : UserSchema ;
12
- }
13
-
14
10
const expandAuthor = async ( poll : PollSchema ) : Promise < Poll | null > => {
15
- return UserModel . findById ( poll . authorId ) . then ( ( author : UserSchema | null ) : Poll | null => {
16
- if ( author ) return _ . merge ( _ . omit ( poll , 'authorId' ) , { author } ) ;
17
- return null ;
18
- } ) ;
11
+ return UserModel . findById ( poll . authorId )
12
+ . lean < User > ( )
13
+ . exec ( )
14
+ . then ( ( author : User | null ) : Poll | null => {
15
+ return author && _ . merge ( _ . omit ( poll , 'authorId' ) , { author } ) ;
16
+ } )
17
+ . catch ( err => {
18
+ console . error ( err ) ;
19
+ return err ;
20
+ } ) ;
19
21
} ;
20
22
21
23
const expandAuthorHook = async ( context : HookContext ) : Promise < HookContext > => {
@@ -24,8 +26,8 @@ const expandAuthorHook = async (context: HookContext): Promise<HookContext> => {
24
26
} ;
25
27
26
28
const expandAuthorManyHook = async ( context : HookContext ) : Promise < HookContext > => {
27
- context . result = await bluebird . map ( context . result , ( poll : any ) => expandAuthor ( poll ) ) ;
28
- console . log ( context . result ) ;
29
+ const polls = await bluebird . map ( context . result , ( poll : any ) => expandAuthor ( poll ) ) ;
30
+ context . result = _ . compact ( polls ) ;
29
31
return context ;
30
32
} ;
31
33
0 commit comments