0

I'm using x-ray library in my MEAN.js project and I can't fix this error message:

Error: write after end at writeAfterEnd (_stream_writable.js:166:12) at Writable.write (_stream_writable.js:211:5) at Writable.end (_stream_writable.js:446:10) at _stream_object (C:\src\je\foodleWeb\node_modules\x-ray\lib\stream.js:46:16) at next (C:\src\je\foodleWeb\node_modules\x-ray\index.js:137:11) at C:\src\je\foodleWeb\node_modules\x-ray\index.js:242:7 at C:\src\je\foodleWeb\node_modules\x-ray\lib\walk.js:56:12 at callback (C:\src\je\foodleWeb\node_modules\batch\index.js:147:12) at C:\src\je\foodleWeb\node_modules\x-ray\lib\walk.js:49:9 at C:\src\je\foodleWeb\node_modules\x-ray\index.js:232:24 at next (C:\src\je\foodleWeb\node_modules\x-ray\index.js:138:11) at C:\src\je\foodleWeb\node_modules\x-ray\index.js:242:7 at C:\src\je\foodleWeb\node_modules\x-ray\lib\walk.js:56:12 at callback (C:\src\je\foodleWeb\node_modules\batch\index.js:147:12) at C:\src\je\foodleWeb\node_modules\x-ray\lib\walk.js:39:11 at C:\src\je\foodleWeb\node_modules\x-ray\lib\walk.js:56:12 at callback (C:\src\je\foodleWeb\node_modules\batch\index.js:147:12) at C:\src\je\foodleWeb\node_modules\x-ray\lib\walk.js:49:9 at C:\src\je\foodleWeb\node_modules\x-ray\index.js:212:18 at next (C:\src\je\foodleWeb\node_modules\x-ray\index.js:138:11) at C:\src\je\foodleWeb\node_modules\x-ray\index.js:242:7 at C:\src\je\foodleWeb\node_modules\x-ray\lib\walk.js:56:12

This is my code where I'm using the built in function "pipe" to pass the response back to $http

export function show(req, res) {
  var stream = x(url,
    [{
      text: '.name'
    }])(function (err, data) {
      if (err) {
        console.log("[Xray] Error:", err);
      }
      else {
        return data;
      }
    }).stream();

  stream.pipe(res);
}

I've tried to use the following approach to catch the exception but it doesn't work fine as it's based on promises. I'd like to have something similar in order to prevent the app to crash.

Maybe I could wrap my code into a function and pass in a callback or something similar...

function respondWithResult(res, statusCode) {
  statusCode = statusCode || 200;
  return function (entity) {
    if (entity) {
      res.status(statusCode).json(entity);
    }
  };
}

function handleError(res, statusCode) {
  statusCode = statusCode || 500;
  return function (err) {
    res.status(statusCode).send(err);
  };
}

export function index(req, res) {
  return Squeme.find().exec()
    .then(respondWithResult(res))
    .catch(handleError(res));
}
Rober
  • 726
  • 8
  • 27
  • I might found a solution by not using the pipe function and building the response this way: return res.status(200).json(data); – Rober Jul 07 '16 at 18:37

1 Answers1

2

I copy my solution from the comments as nobody seems to provide a work around

I might found a solution by not using the pipe function and building the response this way: return res.status(200).json(data);

Rober
  • 726
  • 8
  • 27