0

目前,当我卷曲到我的端点时,我能够检索到响应,但只能检索一次。对我的服务器的任何其他请求都会触发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 
4

1 回答 1

1

不知道 highland.js,我的猜测是错误消息给了你答案,使用观察而不是应用。可能返回

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

calculateTotal().observe([observeFunction])

这样每次调用它时,都会返回要使用的流。现在您正在使用函数中的流。也许这就是为什么当你再次调用它时它会抱怨。

于 2016-05-03T20:28:58.813 回答