A valid status code is required for the POST request in TSOA Typescript. Please ensure it is a numerical

While exploring the possibilities of using tsoa for designing a rest API, I encountered an issue with the post request that always leads to the following error message:

AssertionError [ERR_ASSERTION]: status code must be a number
      at Object.set status [as status] (PROJECT_PATH/node_modules/koa/lib/response.js:87:5)
      at Object.status (PROJECT_PATH/node_modules/delegates/index.js:92:31)
      at Object.<anonymous> (PROJECT_PATH/src/rest/routes.ts:93:31)
      at Generator.next (<anonymous>)
      at PROJECT_PATH/src/rest/routes.ts:8:71
      at new Promise (<anonymous>)
      at __awaiter (PROJECT_PATH/src/rest/routes.ts:4:12)
      at RegisterRoutes.router.post (PROJECT_PATH/src/rest/routes.ts:84:43)
      at dispatch (PROJECT_PATH/node_modules/koa-compose/index.js:42:32)
      at PROJECT_PATH/node_modules/koa-router/lib/router.js:353:16

The sample code segment causing this issue is as follows:

  @Post()
  public async createUser(@BodyProp() requestBody: UserInput): Promise<void> {
    console.log(requestBody)
    // code
    this.setStatus(201);
    return Promise.resolve();
  }

In my configuration within the tsoa.json file, I included "middleware": "koa", since I am utilizing kao.

Answer №1

Encountering a similar error, I found a solution by incorporating the koa-bodyparser middleware. This middleware ensures that the http body is appended to the koa ctx.request object.

The documentation for tsoa did not mention this fix.

Here is an example of how to implement it:

    @Post('')
    public async handleRequest (
        @Request() req: Request,
        @BodyProp('param_1') param1: number
    ): Promise<void> {
        // These will all output the http body
        console.log(param1)
        console.log(req.body)
        console.log(req.ctx.request.body)
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a Map in TypeScript from an Array

I have a series of TypeScript objects structured like this: interface MyObject { id: string, position: number } My goal is to transform this array into a map format where it shows the relationship between id and position, as needed for a future JSON ...

Utilizing a Material UI custom theme in React with Typescript: A step-by-step guide

Upon using the tool , I received a js file and a json file following the paths mentioned on the theme generator page: // src/ui/theme/index.js /* src/ui/theme/theme.json */ The files operate smoothly when left with the .js extension. However, when I attem ...

The ongoing ESLint conundrum: Balancing between "Unused variable" and "Unknown type" errors when utilizing imports for type annotations

I've encountered a linting issue and I need some guidance on how to resolve it. Here's the scenario - when running $ yarn lint -v yarn run v1.22.4 $ eslint . -v v6.8.0 With plugins vue and @typescript-eslint, I have the following code in a .ts ...

Tips for displaying a hyperlink in an Angular 6 component template

In my Angular 6 application, I am facing an issue with a component that is used to display messages on the page. Some of the messages include hyperlinks in HTML markup, but they are not being rendered properly when displayed (they appear as plain text with ...

Material-UI Slide component is encountering an issue where it is unable to access the style property of an undefined variable

Recently, I incorporated Material-UI Slide into my project and I'm curious about why the code functions correctly when written in this manner: {selectedItem && selectedItem.modal && selectedItem.modal.body ? ( selectedItem.modal.body.map((section ...

Using TypeScript, define a React function component by specifying its name as a type

While working in React.js, I encountered an issue with my Function Component. When I attempted to use the function name as the type, it resulted in an error. Here is a snippet of the code: import * as React from "react"; import { render } from "react-dom ...

Cannot trigger event ascn.onchange does not exist as a function

Need to trigger the onChange function and pass parameters within it. Here's what I have tried: setTimeout(function() { document.getElementById(input.key)?.onchange({}) }, 200); Encountering the following error message: cn.onchange is not a function ...

Troubleshooting problem with refreshing URL on "ionic serve" mode

After transitioning my project from Ionic 2 to Ionic 3, I've encountered an issue with ionic serve and the rebuilding process. Initially, when I build the project, everything functions as expected. However, I've noticed that the URL in the brows ...

Identify when the user ceases typing in Angular 2

I am currently working on implementing a feature that detects whether the user is typing or not. I need to determine when the user has stopped typing for at least 3 seconds in order to perform certain actions. I have successfully detected when the user sta ...

Using regular expressions to identify sentences containing a list of stopwords

In order to locate sentences that might include a series of stopwords mixed within the phrase to_match, such as: make wish make a wish make the a wish let stopword: string[]= ["of", "the", "a"]; let to_match : string = " ...

Elevate the scope analysis for a function within the Jasmine framework

I have written a few functions within the app component. I am experiencing an issue with increasing coverage in the summary for these component methods. The test cases are functioning correctly, but some lines are not being accounted for in the coverage s ...

Utilize Angular to initiate the transmission of attribute values upon a click event

My question may be simple, but I've been struggling to find an answer. How can I send attributes or item property bindings of an item through a click event in the best way? For example: <item class="item" [attr.data-itemid]="item.id ...

Error encountered with TypeScript compiler when using a React Stateless Function component

I am attempting to create a React Stateless Function component using TypeScript. Take a look at the code snippet below: import * as React from 'react'; import {observer} from 'mobx-react'; export interface LinkProps { view: any; ...

Launching a Dialog in Angular upon selection of an option

Within my application, I have a mat-form-field that includes a mat-option. My goal is for a dialog to open whenever I select an element from the mat-option. However, at the moment, nothing happens when I make a selection. What steps should I take to resolv ...

Encountering a 404 error when importing http/server in deno

The file named index.ts is located below import { serve } from "https://deno.land/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0c3c4d4f0809e8186869e80">[email protected]</a>/http/server.ts"; function ...

Testing Slack's Web API with Jest for mock purposes

Currently, I am working with two files. One file is where I set up a slack web API client to post a message and test it with a mocked value: main.ts: import { WebClient } from '@slack/web-api'; const slack = new WebClient(process.env.SLACK_API_K ...

What is the best way to bring in a variable initialized by an IIFE from a JavaScript file into a TypeScript class?

I'm currently working towards integrating the steelseries.js library (found at https://github.com/HanSolo/SteelSeries-Canvas) into a Grafana plugin built with React. It's quite a complex task, but I'm up for the challenge. Right now, my ma ...

Why does my array seem to update only once in the view?

I am currently working on a project that aims to visually represent sorting algorithms, but I have encountered an issue. In order to effectively visualize the sorting process of an algorithm, it is crucial to display every change in the array as the proc ...

Creating a function within a module that takes in a relative file path in NodeJs

Currently, I am working on creating a function similar to NodeJS require. With this function, you can call require("./your-file") and the file ./your-file will be understood as a sibling of the calling module, eliminating the need to specify the full path. ...

What causes the variable to be undefined in the method but not in the constructor in Typescript?

I am currently working on an application using AngularJS 1.4.9 with Typescript. In one of my controllers, I have injected the testManagementService service. The issue I'm facing is that while the testManagementService variable is defined as an object ...