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.

TypeError: next is not a function

See original GitHub issue

I’m trying to log JSON data in an array returned from REST API and getting “TypeError: next is not a function” error. Data is logged in a file but getting that error message. Any suggestion is appreciated.

webService.getData({product_id: 'GOOG'}, (error, response, data) => {
		if (error) {
			// handle the error
		} else {
			data.forEach(fill => {				
				if (typeof fill === 'object') {
					logger.info(JSON.stringify(fill));
				}
				
			})

		}
	})

I’m logging data returned from REST API

  next();
  ^

TypeError: next is not a function
    at WriteStream.onDrain (C:\Users\nash\wsclient\node_modules\winston\lib\winston\transports\file.js:390:3)
    at emitNone (events.js:91:20)
    at WriteStream.emit (events.js:185:7)
    at onwriteDrain (_stream_writable.js:397:12)
    at afterWrite (_stream_writable.js:385:5)
    at onwrite (_stream_writable.js:378:7)
    at WritableState.onwrite (_stream_writable.js:90:5)
    at fs.js:2195:5
    at FSReqWrap.wrapper [as oncomplete] (fs.js:2162:5)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
mempfcommented, Oct 16, 2017

I have been able to reproduce the issue mentioned by @nengine by feeding in a very large string into the logger.info() call.

After debugging for a while I noticed a possible “this” context issue with the winston file transport.

File.prototype._onDrain = function onDrain() {
  if (--this._drains) return;
  var next = this._next;
  this._next = noop;
  next();
};

I noticed that “this._drains” was undefined and becomes NaN after doing the decrement. “this._next” is also undefined which leads to the error mentioned when executing the “next()” call.

  this._dest = fs.createWriteStream(fullpath, this.options)
    .on('error', function (err) {
      // TODO: what should we do with errors here?
      debug(err);
    })
    .on('drain', this._onDrain)
    .on('open', function () {
      debug('file open ok', fullpath);
      self.emit('open');
      self._stream.pipe(self._dest);
    });

The code that sets up the drain event listener will cause the “this” context of the “this._onDrain” to be the newly created “fs.createWriteStream” stored in this._dest property instead of the File transport “this” context.

Changing to: .on('drain', () => this._onDrain()) in the above code seems to solve the issue. By wrapping in an arrow function the “this” context is preserved. This implies that the “this” context is being set wrong when the “this._onDrain” function is called on a “drain” event.

So I believe this is a legitimate bug in the File transport code unless someone thinks otherwise.

2reactions
nenginecommented, Oct 16, 2017

Using @next version. Please see below for Winston config. Thanks.

const winston = require('winston');
const { format } = require('logform');

const logFormat = format.printf(info => {
  return `[${info.timestamp}] ${info.level}: ${info.message}`;
});

const logger = winston.createLogger({
  level: 'info',
  format: format.combine(
  	format.timestamp(),
  	logFormat
  ),    
  transports: [
    new winston.transports.File({ filename: './logs/error.log', level: 'error' }),
    new winston.transports.File({ filename: './logs/application.log', maxsize: 104857600 }),    
  ]
});

if (process.env.NODE_ENV !== 'production') {
  logger.add(new winston.transports.Console({
    format: format.combine(
	  	format.timestamp(),
	  	logFormat
  	)
  }));
}


Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: next is not a function - javascript
I'm running a Node.js-server and trying to test this Rest API that I made with Express. It's linked up to MongoDB using Mongoose....
Read more >
next is not a function TypeError: next is not a function #3407
Describe the bug After cloning the repo and running docker-compose up, I'm getting the following error message when clicking on log in ...
Read more >
TypeError: "x" is not a function - JavaScript - MDN Web Docs
The JavaScript exception "is not a function" occurs when there was an attempt to call a value from a function, but the value...
Read more >
JavaScript: Uncaught TypeError: n is not a function
This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the...
Read more >
Writing middleware for use in Express apps
Calling this function invokes the next middleware function in the app. The next() function is not a part of the Node.js or Express...
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