search

Home  >  Q&A  >  body text

mongodb - How to pass values ​​between two middlewares in koa?

How to transfer values ​​between two middlewares

test.post('/', koaBody, function* (next) {
    var body = this.request.body;
    console.log(body);
    yield body;
    yield next;
})
app.use(function* (next) {

  this.body="sucessful";
  console.log("中间件2");
  yield this.mongo.db('app_info').collection('platform').insert({"xiaoming":"xiaoming1"});
  this.body=  this.mongo.db('app_info').collection('platform').findOne();
});

As above, I want to pass the body obtained by the post of the first middleware into the second middleware, and then store it in monodb by the second middleware. How should I obtain it?

巴扎黑巴扎黑2788 days ago598

reply all(2)I'll reply

  • 为情所困

    为情所困2017-05-02 09:26:15

    Transfer value to global variables

    reply
    0
  • ringa_lee

    ringa_lee2017-05-02 09:26:15

    You need to hang it on ctx first, and then get it in the next middleware!

    reply
    0
  • Cancelreply