I am writing my first backend for the RethinkDB database and have encountered such a problem that I cannot parse the query in bodyParser

Here is my code:

const Koa = require('koa') const logger = require('koa-morgan') const bodyParser = require('koa-bodyparser') const Router = require('koa-router') const r = require('rethinkdb') const server = new Koa() const router = new Router() const db = async() => { const connection = await r.connect({ host: 'localhost', port: '28015', db: 'getteamDB' }) return connection; } server.use(bodyParser()); server .use(router.routes()) .use(logger('tiny')).listen(3001) const getUsers = async(ctx, next) => { await next() // Get the db connection. const connection = await db() // Check if a table exists. var exists = await r.tableList().contains('users').run(connection) if (exists === false) { ctx.throw(500, 'users table does not exist') } // Retrieve documents. var cursor = await r.table('users') .run(connection) var users = await cursor.toArray() ctx.type = 'json' ctx.body = users } const insertUser = async(ctx, next) => { await next() // Get the db connection. const connection = await db() // Throw the error if the table does not exist. var exists = await r.tableList().contains('users').run(connection) if (exists === false) { ctx.throw(500, 'users table does not exist') } let body = ctx.request.body || {} console.log('получаем имя в запросе - ', body); // Throw the error if no name. if (body.name === undefined) { ctx.throw(400, 'name is required') } // Throw the error if no email. if (body.email === undefined) { ctx.throw(400, 'email is required') } let document = { name: body.name, email: body.email } var result = await r.table('users') .insert(document, {returnChanges: true}) .run(connection) ctx.body = result } router .get('/users', getUsers) .post('/users', insertUser) 

The getUsers method works fine, returns an empty array (the database is empty). The insertUser method cannot get the body out of the route.

Tell me where the error is

Console log writes {} And logger writes 'name is required'

    1 answer 1

    Instead:

      let body = ctx.request.body || {} 

    used:

      let body = ctx.request.query || {} 

    The solution in this topic on SO helped.