Created
January 30, 2019 00:57
-
-
Save THEozmic/4dc55b0f3d4c8bf73ea56b0b679165b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mongoose from 'mongoose' | |
import '../utils/db' | |
import Posts from './postsModel' | |
import { verifyToken, respondWith } from '../utils' | |
export const handler = async (event, context) => { | |
context.callbackWaitsForEmptyEventLoop = false | |
if (event.httpMethod !== 'POST') return respondWith({ statusCode: 200 }) | |
try { | |
const data = JSON.parse(event.body), | |
username = data.username, | |
content = data.content, | |
profile_image = data.profile_image, | |
id = mongoose.Types.ObjectId(), | |
post = { | |
_id: id, | |
username, | |
profile_image, | |
content, | |
}, | |
response = { | |
msg: "Post successfully created", | |
post, | |
} | |
try { | |
await verifyToken(event.headers.token) | |
} catch (e) { | |
throw { | |
statusCode: 401, | |
message: e.message | |
} | |
} | |
await Posts.create(post) | |
return respondWith({ statusCode: 201, response }) | |
} catch (err) { | |
console.log(err) // output to netlify function log | |
return respondWith({ statusCode: err.statusCode || 500, response: { msg: err.message } }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment