N
N
Nicolas2015-10-02 22:51:11
JavaScript
Nicolas, 2015-10-02 22:51:11

How to make a redirect to koa.js after a POST request?

Using koa-router I make a route:

router.post('/registration', function* (next) {
    var ctx = this;

    models.users.create({
       email: this.request.body.email
    }).then(function() {
      ctx.redirect('/');
    });
  })

After successfully creating an entry in the database, I try to make a redirect, but I get an error:
Error: Can't set headers after they are sent.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicolas, 2015-10-03
@Achan

It was necessary to add yield.

router.post('/registration', function* (next) {
    var ctx = this;

    yield models.users.create({
       email: this.request.body.email
    }).then(function() {
      ctx.redirect('/');
    });
  })

I don't quite understand how it works. Someone can explain?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question