无法使用流查询多次

Unable to query more than once using streams

目前,当我卷曲到我的端点时,我能够检索响应,但只能检索一次。对我的服务器的任何额外请求都会触发 stream error: Stream already being consumed, you must either fork() or observe()

我的堆栈:node、express、highlandjs,mongodb。

//server.js     
app.get('/queries', query.calculateTotal);

//我的端点函数

var _ = require('lodash')
var sg = require("reactive-superglue")
var query = sg.mongodb("mongodb://localhost:27017/qatrackerdb").collection("test1")

exports.calculateTotal = function (err, res) {
    query.find()
        .collect()
        .map(function(x) {
            console.log(x)
            return _.size(x)
        })
        .apply(function(x) {
            return res.status(200).json(x)
        })
}

第二次尝试访问我的端点后服务器响应:curl -i -X GET http://localhost:3000/queries/

GET /queries/ 200 34.442 ms - 632
GET /queries/ - - ms - -
GET /queries/ 500 2.371 ms - 1998
Error: Stream already being consumed, you must either fork() or observe()
    at Stream._addConsumer 

不知道 highland.js,我的猜测是错误消息给了你答案,请使用 observe 而不是 apply。可能 returning

query.find() .collect() .map(function(x) { console.log(x) return _.size(x) }) 并让 calculateTotal 函数的请求者观察 return:

calculateTotal().observe([observeFunction])

通过这种方式,每次调用它时,您都在 return 使用要使用的流。现在您正在使用函数中的流。也许这就是为什么当你再次调用它时它会抱怨的原因。