I have an angular2 client HTTP request being thrown to a node.js server which is fetching a huge collection(~ 2 Million documents) from MongoDB and sending it as streamed response to the client.
Problem: works fine when the collection is small.but with a large collection, the client shows JSON parse errors.
Server side excerpt:
var first = true;
res.setHeader("Content-Type", "application/json");
res.write('"data" : [');
var stream = Datasource.db.collection(*collectionName*).find(*filterJson*).stream();
stream.on('data', function(doc) {
var prefix = first ? '' : ', ';
res.write(prefix + JSON.stringify(doc));
console.log(prefix +JSON.stringify(doc));
first = false;
});
stream.on('end', function() {
res.write(']}');
console.log(']}');
res.end();
});
client side err :
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Response.Body.json (http.umd.js:777)
at MapSubscriber.eval [as project] (datasources.services.ts:28)
at MapSubscriber._next (map.ts:75)
at MapSubscriber.Subscriber.next (Subscriber.ts:95)
at XMLHttpRequest.onLoad (http.umd.js:1180)
at ZoneDelegate.invokeTask (zone.js:275)
at Object.onInvokeTask (core.umd.js:3971)
at ZoneDelegate.invokeTask (zone.js:274)
at Zone.runTask (zone.js:151)
at XMLHttpRequest.ZoneTask.invoke (zone.js:345)
Here the snapshot of chrome's debugging tool:
How can I make my client listen for any amount of streamed response. Can I increase response time somehow.? I tried doing response.timeout = 300000 sec., but no help.
via Nikhil

No comments:
Post a Comment