diff options
author | Marvin Borner | 2018-10-30 20:49:12 +0100 |
---|---|---|
committer | Marvin Borner | 2018-10-30 20:49:12 +0100 |
commit | ee05d577636b80ff58906b9de44f147484fd459e (patch) | |
tree | fac1e04cd032c0f17470a1de68d521524d11ec92 | |
parent | f585fea9f15958260e6af29feb1e9cc72bde64e3 (diff) |
-rw-r--r-- | src/hooks/populate-user.js | 11 | ||||
-rw-r--r-- | src/hooks/process-post.js | 8 |
2 files changed, 2 insertions, 17 deletions
diff --git a/src/hooks/populate-user.js b/src/hooks/populate-user.js index 177bd9f..97d69db 100644 --- a/src/hooks/populate-user.js +++ b/src/hooks/populate-user.js @@ -3,22 +3,15 @@ module.exports = function (options = {}) { // eslint-disable-line no-unused-vars return async context => { - // Get `app`, `method`, `params` and `result` from the hook context const {app, method, result, params} = context; - - // Make sure that we always have a list of posts either by wrapping - // a single post into an array or by getting the `data` from the `find` method's result const posts = method === 'find' ? result.data : [result]; - // Asynchronously get user object from each post's `userId` - // and add it to the post await Promise.all(posts.map(async post => { - // Also pass the original `params` to the service call - // so that it has the same information available (e.g. who is requesting it) post.user = await app.service('users').get(post.userId, params); })); - // Best practice: hooks should always return the context + posts.forEach(post => delete post.userId); + return context; }; }; diff --git a/src/hooks/process-post.js b/src/hooks/process-post.js index 553bb59..70e8397 100644 --- a/src/hooks/process-post.js +++ b/src/hooks/process-post.js @@ -6,28 +6,20 @@ module.exports = function (options = {}) { return async context => { const {data} = context; - // Throw an error if we didn't get a text if (!data.text) { throw new Error('A post must have a text'); } - // The authenticated user const user = context.params.user; - // The actual message text const text = context.data.text - // Posts can't be longer than 400 characters .substring(0, 400); - // Override the original data (so that people can't submit additional stuff) context.data = { text, - // Set the user id userId: user.id, - // Add the current date createdAt: new Date().getTime() }; - // Best practise, hooks should always return the context return context; }; }; |