NodeJs - 有没有办法限制使用await&Promise连接到远程服务器时进行的调用/尝试次数



所以我遇到了一个问题,我试图使用 async、await 和 Promise 连接到 Nodejs 中的远程服务器。

Error: connect ETIMEDOUT 10.1.239.44:80
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)

问题是,当服务器不可用并且发生上述错误时,我的请求不会返回状态,而是不断尝试定期连接到服务器,如下所示-

Do the POST call
ERROR>>>>Error: connect ETIMEDOUT 10.1.239.44:80
Do the POST call
ERROR>>>>Error: connect ETIMEDOUT 10.1.239.44:80
Do the POST call
ERROR>>>>Error: connect ETIMEDOUT 10.1.239.44:80
Do the POST call
ERROR>>>>Error: connect ETIMEDOUT 10.1.239.44:80
Do the POST call
ERROR>>>>Error: connect ETIMEDOUT 10.1.239.44:80

下面是我的代码片段

前端 (Reactjs(-

submit = async() => {
this.spinner("show");
var fileInput = document.getElementById('file-input');
if(fileInput.files.length === 0){
alert("Select at least one file to upload.");
}else{
for (var i = 0; i < fileInput.files.length; i++) {
const upDoc = await this.uploadDocument(fileInput.files[i].name)
.then(document => {
this.setState({ document: document.document })
this.displayDocumentInfo();
this.spinner("hide");
this.reload();
this.setState({ uploaded: !this.state.uploaded });
})
.catch(err => {
this.spinner("hide");
alert(err);
console.log("ERROR111>>>>"+err);
});
console.log("NI Nmber>>>"+this.state.document["NI Number"]);
}
}
}

后端(NodeJs( -

app.get('/uploadDocument/:appId/:file', async (req, res)  => {
var document  = await datacapconnection.uploadDocument(req.params.file,req.params.appId)
.then(document =>{
console.log("DOCUMENT >>> "+JSON.stringify(document));
var jsondata = JSON.parse(document);
if(jsondata.hasOwnProperty('DocId')){
var docId = jsondata.DocId;
var financeInfo  = dbconnection.saveFinacialInfo(document);
console.log("Finance Info >>>"+JSON.stringify(financeInfo));
var docInfo = dbconnection.insertDocumentInfo(docId,req.params.appId,req.params.file);
}
res.json({ document: JSON.parse(document) });
})
.catch((err) => {console.log("ERROR>>>>"+err)});
});

从上面的代码中,我能够从函数datacapconnection.uploadDocument中获取错误并将其打印在catch块中。

const express = require('express');
var http = require('http');
const app = express();
var request = require('request');
var FormData = require('form-data');
var fs = require('fs');
const dirName = 'C:\Users\abhinav.a.mehrotra\Desktop\DataCap\PaySlips\';
var uploadDocument = function(file, appId){
return new Promise((resolve, reject) =>{
jsonObject = JSON.stringify({
"file" : file
});
var postheaders = {
'Content-Type' : 'Content-Type: multipart/form-data;boundary=----WebKitFormBoundaryyrV7KO0BoCBuDbTL'
};
const formData = {
file: fs.createReadStream(dirName + file),
};
var payload = {
url: 'http://10.1.239.45/datacapture/fn/digidocs/submitAsTransaction/'+appId,
formData: formData,
headers: postheaders
}
console.log('Do the POST call');
var proxyRequest = request.post(payload, function(err,res,body) {
if(err){
reject(err);
return
}

resolve(body);
});
})
}
exports.uploadDocument = uploadDocument;

我怎样才能确保只有一个电话出去。如果有任何错误,它会返回到我的前端,而不是继续等待连接发生。任何帮助都非常感谢。

要完成请求周期,您必须在某个时候发送响应。在您的情况下,当它出现错误时 - 您要做的就是console.log哪个打印错误。我不确定为什么它会重复错误,watcher(nodemon(可能是原因。

查询的解决方案是,每当函数得出结论时发送响应,可能是正面响应,也可能是负面响应。在catch块中添加以下行以指示请求失败:

// some process
.catch(function(error) {
res.status(500)
.json({
success: false,
message: typeof error.message && error.message === "string" ? error.message: "Technical error! Please contact support."
})
})

最新更新