我正在创建一个带有expressjs的应用程序,该应用程序将实时数据从mongodb发送到javascript客户端,该客户端定期请求Web服务。 应用程序目标是使用高图表库显示它们。
问题是我可以将数据传递给客户端。
我的问题是:
-
为什么响应对象在getLiveData()函数中是正确的,然后在调用时变得未定义
app.get("/liveDataRequest", function(req, res){ etc ... ?
-
为什么requestData()函数只有在注释以下两行时才被调用:
dataType: 'json', contentType: 'application/json',
这些是我的代码的相关部分:
服务器端代码:服务器.js
function getLiveData(){ var liveArray=[]; var response; const changeStream = dbObject.collection('status').watch(pipeline); // start listening to changes changeStream.on("change", function(change) { console.log('in change stream'); liveArray.push([change.fullDocument.ts , change.fullDocument.T]); response = { "liveArray" : liveArray }; console.log("from getLiveData : " , reponse); return response; }); } app.get("/liveDataRequest", function(req, res){ console.log('Debug 10'); var liveResponse=getLiveData(); console.log("liveResponse : " , liveResponse); res.status(200).send(liveResponse); console.log('Debug 20'); });
客户端代码:客户端.js
function requestData() { $.ajax({ url: 'http://localhost:3300/liveDataRequest', type: 'GET', //dataType: 'json', //contentType: 'application/json', success: function(point) { var series = myChart.series[0], shift = series.data.length > 20; // shift if the series is // longer than 20 // add the point myChart.series[0].addPoint(point.liveArray[0][1], true, shift); // call it again after one second setTimeout(requestData, 1000); } }); }
这是控制台输出,它表明在发送到客户端之前,响应对象变得未定义:
Debug 10
liveResponse : undefined
Debug 20
in change stream
from getLiveData : { liveArray:
[ [ Fri Apr 13 2018 01:39:26 GMT-0400 (EDT), 24.5 ],
[ Fri Apr 13 2018 01:39:28 GMT-0400 (EDT), 24.8 ] ] }
in change stream
from getLiveData : { liveArray:
[ [ Fri Apr 13 2018 01:39:25 GMT-0400 (EDT), 24.5 ],
[ Fri Apr 13 2018 01:39:26 GMT-0400 (EDT), 24.5 ],
[ Fri Apr 13 2018 01:39:28 GMT-0400 (EDT), 24.8 ] ] }
Debug 10
liveResponse : undefined
Debug 20
Debug 10
liveResponse : undefined
Debug 20
控制台日志捕获
PS:我是使用nodejs和javascript(以及一般的Web技术)的新手。
编辑:现在我正在使用一个回调函数,其中包含getLiveData()函数,并且出现以下错误:
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
at ServerResponse.header (/home/osboxes/Desktop/node-highcharts-10/node_modules/express/lib/response.js:767:10)
at ServerResponse.send (/home/osboxes/Desktop/node-highcharts-10/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/home/osboxes/Desktop/node-highcharts-10/node_modules/express/lib/response.js:267:15)
at ServerResponse.send (/home/osboxes/Desktop/node-highcharts-10/node_modules/express/lib/response.js:158:21)
at /home/osboxes/Desktop/node-highcharts-10/server2.js:242:21
at null.<anonymous> (/home/osboxes/Desktop/node-highcharts-10/server2.js:200:9)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at processNewChange (/home/osboxes/Desktop/node-highcharts-10/node_modules/mongodb/lib/change_stream.js:348:49)
新代码 :
app.get("/liveDataRequest", function(req, res){
console.log('Debug 10');
getLiveData(function(data){
res.status(200).send(data);
//res.json(data); //i get same error when i use this too
console.log("in handler", data);
});
console.log('Debug 20');
});
问题一:
getLiveData()
在这里不返回任何内容。 您的return
发生在changeStream.on(...)
内
您可以在此处使用callbacks
或promises
。
回调示例:
function getLiveData(handler){
changeStream.on('change', function(change){
//...
handler(response);
});
}
getLiveData(function(data){
res.send(data);
});
承诺示例:
function getLiveData(){
return new Promise(function (resolve, reject) {
changeStream.on('change', function(change){
//...
resolve(response);
}).on('error', reject);
});
}
getLiveData()
.then(function (data) {
res.send(data)
})
.catch(console.error.bind(console));
问题二:
该函数可能确实被调用,但由于返回的响应JSON
无效(它将undefined
),因此成功回调不会被触发(错误回调将被触发)。
可以通过查看开发工具的"network
"选项卡来验证这一点。