diff options
Diffstat (limited to 'services/polls')
-rw-r--r-- | services/polls/polls.hooks.ts | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts index ff6a83f..fc5fa45 100644 --- a/services/polls/polls.hooks.ts +++ b/services/polls/polls.hooks.ts @@ -2,20 +2,22 @@ import { HookContext } from '@feathersjs/feathers'; import bluebird from 'bluebird'; import _ from 'lodash'; -import { PollSchema } from '../../models/polls/poll.schema'; -import { UserSchema } from '../../models/users/user.schema'; +import { Poll, PollSchema } from '../../models/polls/poll.schema'; +import { User } from '../../models/users/user.schema'; import UserModel from '../../models/users/user.model'; -interface Poll extends Omit<PollSchema, 'authorId'> { - author: UserSchema; -} - const expandAuthor = async (poll: PollSchema): Promise<Poll | null> => { - return UserModel.findById(poll.authorId).then((author: UserSchema | null): Poll | null => { - if (author) return _.merge(_.omit(poll, 'authorId'), { author }); - return null; - }); + return UserModel.findById(poll.authorId) + .lean<User>() + .exec() + .then((author: User | null): Poll | null => { + return author && _.merge(_.omit(poll, 'authorId'), { author }); + }) + .catch(err => { + console.error(err); + return err; + }); }; const expandAuthorHook = async (context: HookContext): Promise<HookContext> => { @@ -24,8 +26,8 @@ const expandAuthorHook = async (context: HookContext): Promise<HookContext> => { }; const expandAuthorManyHook = async (context: HookContext): Promise<HookContext> => { - context.result = await bluebird.map(context.result, (poll: any) => expandAuthor(poll)); - console.log(context.result); + const polls = await bluebird.map(context.result, (poll: any) => expandAuthor(poll)); + context.result = _.compact(polls); return context; }; |