Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'pipesCount' of undefined #14

Open
bwyss opened this issue Dec 20, 2016 · 11 comments
Open

TypeError: Cannot read property 'pipesCount' of undefined #14

bwyss opened this issue Dec 20, 2016 · 11 comments

Comments

@bwyss
Copy link

bwyss commented Dec 20, 2016

I am trying to use run-middleware within an angular-fullstack application.

My application is using express for managing sessions and passport.js for authentication (stored in Mongodb). When a user logins in, I want to check if that user already has a living session. I want to use run-middleware to programmatically query mongodb for all the live sessions.

'use strict';

import path from 'path';

import passport from 'passport';
import {Strategy as LocalStrategy} from 'passport-local';


import express from 'express';
import session from 'express-session';


import _ from 'lodash';
import Session from '../../api/session/session.model';


var app = express();

require('run-middleware')(app);


function localAuthenticate(User, email, password, done, req) {
  User.findOne({
    email: email.toLowerCase()
  }).exec()
    .then(user => {

      if (!user) {
        return done(null, false, {
          message: 'This email is not registered.'
        });
      }

      // HERE is where I am trying to use the runMiddleware 
      app.runMiddleware('/sessions',{},function(code,data){
        console.log(code) // 200 
        console.log(data) // { user: '20', name: 'Moyshale' }
      });

      user.authenticate(password, function(authError, authenticated) {
        if (authError) {
          return done(authError);
        }
        if (!authenticated) {
          return done(null, false, { message: 'This password is not correct.' });
        } else {
          return done(null, user);
        }
      });
    })
    .catch(err => done(err));
}

export function setup(User, config) {

  passport.use(new LocalStrategy({
    passReqToCallback: true,
    usernameField: 'email',
    passwordField: 'password' // this is the virtual field on the model
  }, function(req, email, password, done) {
    return localAuthenticate(User, email, password, done, req);
  }));
}

The error I am getting is:

if (state.pipesCount === 0)
           ^
TypeError: Cannot read property 'pipesCount' of undefined
    at IncomingMessage.Readable.unpipe (_stream_readable.js:634:12)
    at unpipe (/home/enview/node_modules/unpipe/index.js:47:12)
    at send (/home/enview/node_modules/finalhandler/index.js:184:3)
    at Immediate.<anonymous> (/home/enview/node_modules/finalhandler/index.js:113:5)
    at Immediate.<anonymous> (/home/enview/node_modules/express/lib/router/index.js:618:15)
    at runCallback (timers.js:568:20)
    at tryOnImmediate (timers.js:546:5)
    at processImmediate [as _immediateCallback] (timers.js:525:5)


@borisovg
Copy link

borisovg commented Aug 24, 2017

Looks like adding any sort of middleware is enough to break it :(

var express = require('express');
var runMiddleware = require('run-middleware');

var app = express();

app.use(function (req, res, next) {
    console.log({uri: req.originalUrl, method: req.method, ip: req.ip}, 'Request');
    next();
});

app.get('/foo', function (req, res) {
    res.json({result: 'OK'});
});

runMiddleware(app);

app.runMiddleware('/foo', function (code, body) {
    console.log('CODE:', code);
    console.log('BODY:', body);
});

results in

_stream_readable.js:633
  if (state.pipesCount === 0)
           ^

TypeError: Cannot read property 'pipesCount' of undefined
    at IncomingMessage.Readable.unpipe (_stream_readable.js:633:12)
    at unpipe (/home/borisov/test/node-run-middleware/node_modules/unpipe/index.js:47:12)
    at send (/home/borisov/test/node-run-middleware/node_modules/finalhandler/index.js:275:3)
    at Immediate.<anonymous> (/home/borisov/test/node-run-middleware/node_modules/finalhandler/index.js:135:5)
    at Immediate.<anonymous> (/home/borisov/test/node-run-middleware/node_modules/express/lib/router/index.js:635:15)
    at runCallback (timers.js:674:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)

@azaslavsky
Copy link

Anyone have any idea about what to do if this happens?

@deejayres
Copy link

Just ran into this as well.

@alflennik
Copy link

I'm getting this error from an entirely different setup that doesn't even include node-run-middleware. So maybe the problem is deeper - like Express itself.

@netpedro-com
Copy link

I ran into this same issue. Solved by passing the request parameter {connection: {}}. E.g.:

app.runMiddleware('/handler', {connection: {}}, function(code, data) {
  console.log(code, data);
  process.exit();
})

@WeishengChang
Copy link

I solved it by passing original_req parameter:

app.runMiddleware('/another-route', {original_req: req}, function(err, data) {
  ...
});

@therealpecus
Copy link
Contributor

therealpecus commented Nov 21, 2019

this still happens if you have routes configured with sendFile:

app.get(CLIENT_ROUTES, function(req, res) {
  res.sendFile(join(DIST_FOLDER, 'browser', 'index.html'));
});

I tried every solution proposed above to no avail.

@hotlib
Copy link
Contributor

hotlib commented May 26, 2020

I solved it by passing original_req parameter:

app.runMiddleware('/another-route', {original_req: req}, function(err, data) {
  ...
});

this worked for me

@danielponce263
Copy link

Try to delete the response.json() of the api /sessions , it works for me.

@Aminadav
Copy link
Owner

If anyone wants to create a PR to fix it, I will merge it

@dustinbolton
Copy link

dustinbolton commented Nov 20, 2023

I had this issue before I had a catchall route set up to 404. Basically when a url was accessed where no route matched then this happened. Adding a catchall after all other routes were registered (so it only runs if nothing else matched) fixed it in my case. app.all('*', ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests