1

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
})
doums
  • 470
  • 10
  • 24

1 Answers1

0

Using async/await for example :

createPromisified(data) {
  return new Promise((resolve, reject) => {
    post.create(data, (err, ret) => {
      if (err) return reject(err);

      return resolve(ret);
    });
  });
}

router.put('/test', async(ctx, next) => {
  await createPromisified(something);

  ctx.body = newPost;
 
  ctx.status = 200;
})

Using generic code example which represent your case :

function postCreate(data, callback) {
  setTimeout(() => callback(false, 'ret'), 300);
}

function createPromisified(data) {
  return new Promise((resolve, reject) => {
    postCreate(data, (err, ret) => {
      if (err) return reject(err);

      return resolve(ret);
    });
  });
}

async function routerPut(ctx, next) {
  await createPromisified('something');

  console.log(ctx.body);

  ctx.body = 'newData';

  console.log(ctx.body);

  ctx.status = 200;
}


routerPut({
    body: 'hey',
  },

  () => {
    console.log('next');
  },
);
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
  • Hi, ok this working fine thx, but can you explain me something ? `post.create` return already a Promise. So I tested your code but with removing the `return new Promise` which wraps `post.create`, and I return directly the result of the Promise produces by `post.create` instead. But this does not work. Why ? – doums Sep 05 '18 at 12:31
  • 1
    Ok I understand why! It's fine – doums Sep 05 '18 at 12:47