我正在尝试发出 SOAP 请求,并将结果推送到 firebase 数据库。
我编写了一个函数,该函数在使用 node index.js 调用时可以正常工作以 console.log 我的 SOAP 请求的结果。
我有一个在 HTTPS 请求上触发的 firebase 函数。这个函数调用了我的,但没有数据被推送到数据库。
function getStopData(){
(async () => {
const { response } = await soapRequest(url, headers, xml); // Optional timeout parameter(milliseconds)
const { body, statusCode } = response;
parseString(body, function (err, result) {
var envelope = result['soap:Envelope'];
var body = envelope['soap:Body'];
var result = body.pop();
var resultXml = result.GetRealTimeStopDataResponse.pop().GetRealTimeStopDataResult.pop()
var stopData = resultXml['diffgr:diffgram'].pop().DocumentElement.pop().StopData;
var busses = []
for (bus in stopData) {
delete stopData[bus].$
busses.push(stopData[bus]);
}
admin.database().ref('/test14').push({testText: 'test14Text'});
});
})();
}
admin.initializeApp();
exports.test14 = functions.region('europe-west1')
.https.onRequest(async (req, res) => {
getStopData(); //Runs function, but no test is pushed to DB
admin.database().ref('/test14').push({testText: 'test14Text'}); //This is pushed to dB
return res.send("Success");
});
测试字符串是推送的 firebase 函数。常规函数中的测试字符串不是。
提前致谢。
如果 firebase 不适合此应用程序,请提出任何其他建议。我正在尝试创建一个每 2 分钟运行一次的函数来发出 SOAP 请求并存储结果。
编辑:根据第一个答案,我修改了我的代码以匹配建议。这给我留下了不同的错误..
5:25:49.623 PM
test14
SOAP FAIL: Error: timeout of 10000ms exceeded
5:25:49.624 PM
test14
Unhandled rejection
5:25:49.624 PM
test14
Error: timeout of 10000ms exceeded at createError (/srv/node_modules/axios-https-proxy-fix/lib/core/createError.js:16:15) at Timeout.handleRequestTimeout [as _onTimeout] (/srv/node_modules/axios-https-proxy-fix/lib/adapters/http.js:216:16) at ontimeout (timers.js:498:11) at tryOnTimeout (timers.js:323:5) at Timer.listOnTimeout (timers.js:290:5)
5:25:49.631 PM
test14
Function execution took 10712 ms, finished with status: 'crash'
我不确定为什么会出现 SOAP Fail,因为使用 node index.js 运行仍然会产生记录到控制台的正确数据。
MonitoredVehicleJourney_Monitored: [ 'true' ],
MonitoredVehicleJourney_InCongestion: [ 'false' ],
MonitoredVehicleJourney_BlockRef: [ '37006' ],
MonitoredVehicleJourney_VehicleRef: [ '44138' ],
MonitoredCall_VisitNumber: [ '4' ],
MonitoredCall_VehicleAtStop: [ 'false' ],
MonitoredCall_AimedArrivalTime: [ '2019-04-14T18:19:45+01:00' ],
MonitoredCall_ExpectedArrivalTime: [ '2019-04-14T18:19:45+01:00' ],
MonitoredCall_AimedDepartureTime: [ '2019-04-14T18:19:45+01:00' ],
MonitoredCall_ExpectedDepartureTime: [ '2019-04-14T18:19:45+01:00' ],
Timestamp: [ '2019-04-14T17:34:14.97+01:00' ],
LineNote: [ '' ] } ]
当您在 Cloud Functions 上运行代码时,您需要为其运行的时间付费。出于这个原因,Cloud Functions 运行时将在您将响应发送回调用客户端后立即终止容器,您使用return res.send("Success")
.
但是您的 SOAP 调用是异步发生的,并在res.send
运行后继续。在这种情况下,您必须在完成后告诉 Cloud Functions。由于您不这样做,因此 Cloud Functions 可能会随时终止运行您的代码的容器,并且显然在这种情况下会在您的 SOAP 请求完成之前发生。
解决方案是仅在 SOAP 调用的结果返回时才写入响应:
function getStopData(){
(async () => {
const { response } = await soapRequest(url, headers, xml); // Optional timeout parameter(milliseconds)
const { body, statusCode } = response;
parseString(body, function (err, result) {
var envelope = result['soap:Envelope'];
var body = envelope['soap:Body'];
var result = body.pop();
var resultXml = result.GetRealTimeStopDataResponse.pop().GetRealTimeStopDataResult.pop()
var stopData = resultXml['diffgr:diffgram'].pop().DocumentElement.pop().StopData;
var busses = []
for (bus in stopData) {
delete stopData[bus].$
busses.push(stopData[bus]);
}
admin.database().ref('/test14').push({testText: 'test14Text'}).then(() => {
res.send("Success");
});
});
})();
}
admin.initializeApp();
exports.test14 = functions.region('europe-west1')
.https.onRequest(async (req, res) => {
getStopData(res);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句