Search code examples
typescripttypeerrordenooak

Developing an API with Oak from Deno: TypeError - request.body is not a function


I'm learning how to develop an API with Deno's Oak by doing basic queries on a list of products.

Except I have an error that I can't fix

TypeError - request.body is not a function

const addProduct = async(
    { request, response } : { request: any, response: any },
) => {
    const body = await request.body(); // <-- here

    if (!request.hasBody) {
        response.status = 400;
        response.body = {
            success: false,
            msg: "No data",
        };
    }
    else {
        const product: Product = body.value;
        products.push(product);
        response.status = 201;
        response.body = {
            success: true,
            data: product,
        };
    }
};

Having other requests in my code I am sharing only part of it with you

export { getProducts, getProduct, addProduct, updateProduct, deleteProduct };

To call it in route.ts: router.get('/api/v1/products', addProduct) then server.ts :

const port = 5000;
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
console.log('Server running on port ${port}');
await app.listen({ port });

Please let me know if you need any further information.

PS: to test my queries I use Postman

I obviously tried to remove the parentheses but in this case it does not retrieve the body inserted on Postman (raw) and just returns success true. I tried to follow parallel solutions found but not being an expert, I cannot understand and adapt them


Solution

  • I found a solution ! The extension Deno on VsCode choose the latest version of Oak when import but in the last I think that body() is not a function of request anymore. So I take the v6.5.0, no problem for the moment.

    Also maybe is not the solution but it keeps me going