I have this code:
router.put('/test', (ctx, next) => {
post.create(something, (err, newPost) => {
if (err) return
ctx.body = newPost
})
console.log(ctx.body) // => undefined
ctx.status = 200
})
The problem is the value I set in the callback to the ctx.body variable is lose outside of the callback.
And I fail to get it works. I tried bind
/async await
but without success.
Can you help me ?
EDIT: @CertainPerformance, the "duplicate" post you linked does not answer my question because the solution it proposes includes to modify directly the signature of function which produces the promise post.create
in my case. And I can't simply do that because it is part of Mongoose API. I read the entire post and I didn't found a solution. So what we do with this post?
EDIT: Based on the answer below, I found two solutions:
router.put('/test', async (ctx, next) => {
const newPost = await Post.create(something).then(post => post)
ctx.body = newPost
ctx.status = 200
})
and
router.put('/test', async (ctx, next) => {
const newPost = new Post(something)
await newPost.save()
ctx.body = newPost
ctx.status = 200
})