Hi,
Awesome middleware you made here!
I'm starting with koa and your middlewares seem pretty great so i'm using koa-better-router
& koa-better-body
together.
However things are not working as expected. I'm
import koa from 'koa';
import body from 'koa-better-body';
import Router from 'koa-rest-router';
import convert from 'koa-convert';
const app = new koa();
apiRouter.resource('users', {
index: [
(ctx, next) => {
ctx.body = "list users!\n";
return next();
},
function *(next) {
this.body = `${this.body} and blah blah..`;
yield next;
}
],
recent: (ctx, next) => { ctx.body = "Newly created user!\n"; return next(); },
create: (ctx, next) => { ctx.body = "Create users!\n"; return next(); },
edit: (ctx, next) => { ctx.body = "Edit user view!\n"; return next(); },
update: [body,
(ctx, next) => {
ctx.body = "create users!\n";
return next();
},
function *(next) {
this.body = `And incoming data is ...`;
this.body = `body from req ${this.body} ${this.request.fields}`;
yield next;
}
],
remove: (ctx, next) => { ctx.body = "remove users!\n"; return next(); }
}, {map: {'new': 'recent'}, methods: {'put': 'post'}});
const basic = new Router(); //- For routes not beginning with /api/v1
basic.extend(apiRouter);
app.use(convert(body())); //- did this out of desperation
app.use(apiRouter.middleware());
app.use(basic.middleware());
This code is from chunks of your koa-better-router
examples and problem comes when i use update
which is using body.
If i include "body" as the beginning arg in update
and post something to /users/1
i get Not Found and nothing gets run cause update
controller wasn't the one called (no controller was).
update: [body,
(ctx, next) => {
ctx.body = "create users!\n";
return next();
},
function *(next) {
this.body = `And incoming data is ...`;
this.body = `body from req ${this.body} ${this.request.fields}`;
yield next;
}
],
If i remove the "body" from the update
and call this.request.fields
i get error.
Update code when i remove body
update: [
(ctx, next) => {
debugger;
ctx.body = "create users!\n";
return next();
},
function *(next) {
this.body = `body from req ${this.request.fields}`;
console.log(this.request);
yield next;
}
],
Error Msg when i remove body
TypeError: Cannot convert object to primitive value
at Object._callee2$ (/home/users/Projects/koa/test-project1/dist/index.js:70:54)
at tryCatch (/home/users/Projects/koa/test-project1/node_modules/regenerator-runtime/runtime.js:64:40)
at Generator.invoke [as _invoke] (/home/users/Projects/koa/test-project1/node_modules/regenerator-runtime/runtime.js:355:22)
at Generator.prototype.(anonymous function) [as next] (/home/users/Projects/koa/test-project1/node_modules/regenerator-runtime/runtime.js:116:21)
at onFulfilled (/home/users/Projects/koa/test-project1/node_modules/co/index.js:65:19)
at /home/users/Projects/koa/test-project1/node_modules/co/index.js:54:5
at Object.co (/home/users/Projects/koa/test-project1/node_modules/co/index.js:50:10)
at converted (/home/users/Projects/koa/test-project1/node_modules/koa-convert/index.js:17:15)
at dispatch (/home/users/Projects/koa/test-project1/node_modules/koa-compose/index.js:44:32)
at next (/home/users/Projects/koa/test-project1/node_modules/koa-compose/index.js:45:18)
Please help.