question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

expressjs - res.status is not a function TypeError

See original GitHub issue

Hello,

I used supertest with tape, sinon, proxyquire & expressJS, I got this error :

error: res.status is not a function TypeError: res.status is not a function
    at <path>\index.ts:26:11
    at process._tickDomainCallback (internal/process/next_tick.js:129:7)
(node:21020) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: res.sendStatus is not a function

this is my route function

router.get('/customers', middleware, (req: express.Request, res: express.Response) => {
  let options = {
    uri: url,
    method: 'GET',
  };
  request(req, options).then(body => {
    if (body) {
      res.status(200).send(body);
    }
  }).catch((error) => {
    res.sendStatus(500);
  });
});

my unit test

import * as test from 'tape';
import * as sinon from 'sinon';
import * as request from 'supertest';
import * as proxyquire from 'proxyquire';

const requestStub = sinon.stub();

const stubResponse = {
  statusCode: 200,
  body: { body: 'body' }
};

const api = proxyquire('./', { 'api-request': requestStub });
requestStub.returns(Promise.resolve(stubResponse));

test('Get /customers', (assert) => {

  const agent = request.agent(api);

  agent
    .get('/customers')
    .expect(200)
    .set('Accept', 'application/json')
    .end((err, res) => {
      var expectedThings =
        {
          "some" : "data"
        };
      var actualThings = res.body;

      assert.error(err, 'No error');
      assert.same(actualThings, expectedThings, 'Retrieve list of things');
      assert.end();
    });
});

Do you have any idea?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:23
  • Comments:24

github_iconTop GitHub Comments

136reactions
iLyxa3Dcommented, Jul 24, 2019

Fixed, in my case… Just add “next” to the variables list (err, req, res, next)

app.use((err, req, res, next) => { console.error(err); res.status(500).json({error: 'an error occurred'}); });

From documentation: https://expressjs.com/ru/api.html

Error-handling middleware always takes four arguments. You must provide four arguments to identify it as an error-handling middleware function. Even if you don’t need to use the next object, you must specify it to maintain the signature. Otherwise, the next object will be interpreted as regular middleware and will fail to handle errors.

37reactions
nikhedoniacommented, Jan 11, 2018

I encountered the same error when applying supertest on a express route.

const routes = express.Router();
routes.get('/', ()=> res.status(200).send('blub'))
request(routes).get('/').end() // status not a function
request(app.use(routes)).get('/').end() // works
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: res.status is not a function - Stack Overflow
When he named the promise.then response as res , the .then scope assumes the res is from resolved promise, not from express route....
Read more >
expressjs - res.status is not a function TypeError · Issue #416
Hello, I used supertest with tape, sinon, proxyquire & expressJS, I got this error : error: res.status is not a function TypeError: ...
Read more >
Node.js Connector TypeError: res.status is not a function
I am learning JS scripting and am making a RESTful API with the MariaDB Node.js Connector and Express.js. But I am stuck on...
Read more >
res.json, res.status, res.redirect, res.render, res.send is not a ...
In this video, I have explained how we can handle errors such as res.send is not a function in express js. In the...
Read more >
TypeError: response.json is not a function in JavaScript
The "response.json is not a function" error occurs when we call the json() method on an object that is not the Response object...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found