我需要从 NodeJS 函数中捕获响应(我的意思是检查 NodeJS 函数的响应何时返回客户端,而不是错误)。
单击按钮时,会启动一个带有表单的函数,NodeJS 在查询后返回一个 csv 文件。问题是查询很复杂,需要 1 分钟以上才能完成。
我需要在单击按钮时启动加载微调器,当 CSV 从函数返回时它应该停止。关于我该怎么做的任何线索?提前致谢。
HTML 表单
<form name="csvForm" ng-submit="download(csvForm.$valid)">
<div class="panel-body">
<div class="row">
<div class="form-group col-sm-4">
<label class="control-label">Concessionaria<span ng-if="userProfileID != 1">*</span></label>
<select class="form-control" ng-model="Dealer"
ng-options="dealer.ID as dealer.Name for dealer in dealers "
ng-required="userProfileID != 1">
<option value=""></option>
</select>
</div>
<div class="form-group col-sm-4">
<label class="control-label">Anno*</label>
<select class="form-control" ng-model="Year"
ng-options="y as y for y in syears" required>
<option value=""></option>
</select>
</div>
</div>
</div>
<div class="form-group col-sm-4 col-sm-offset-4" style="margin-top:20px">
<button name="submitBtn" class="btn btn-lg btn-primary btn-block" type="submit" >Scarica CSV</button>
</div>
</form>
角
$scope.download = function () {
var form = document.createElement('form');
form.action = apihost + "/queryReport/avanzEvaluation";
form.method = 'POST';
form.target = '_blank';
form.style.display = 'none';
var jsonData = {
dealer: $scope.Dealer,
year: $scope.Year
};
var inputJson = document.createElement('input');
inputJson.type = 'text';
inputJson.name = 'data';
inputJson.value = JSON.stringify(jsonData);
var submit = document.createElement('input');
submit.type = 'submit';
submit.id = 'submitProject';
form.appendChild(inputJson);
form.appendChild(submit);
document.body.appendChild(form);
//Send form.
form.submit();
document.body.removeChild(form);
};
节点JS
router.post('/avanzEvaluation', function (req, res) {
return new Promise(function(resolve,reject) {
//Not the real query, just an example
var sql = "SELECT * FROM Table ";
return models.sequelize.query(sql, {replacements: replacements, type: models.sequelize.QueryTypes.SELECT })
.then(function (results) {
const Json2csvParser = require('json2csv').Parser;
//Not real fields, just examples
const fields = ["Field1", "Field2", "Field3", "Field4",];
const opts = { fields };
try {
const parser = new Json2csvParser(opts);
const csv = parser.parse(results);
var filename = ("avanzValutazioni.csv");
res.header("Content-Disposition","attachment;filename=" + filename);
res.setHeader('content-type', 'text/csv');
res.send(iconv.encode(csv, 'iso-8859-1'));
}
catch (err) {
console.error(err);
}
})
.catch(function (err) {
console.log(err);
});
})
});