我有一个Node.js系统,该系统将大量对象上载到MongoDB,并在保管箱中为每个对象创建文件夹。每个对象大约需要0.5秒。因此,在我有很多物体的情况下,这可能需要一分钟左右的时间。我当前要做的是使用202
响应代码通知客户端对象数组已被接受。但是,我如何在一分钟后通知客户完成情况。
app.post('/BulkAdd', function (req, res) {
issues = []
console.log(req.body)
res.status(202).send({response:"Processing"});
api_functions.bulkAdd(req.body).then( (failed, issues, success) => {
console.log('done')
})
});
bulkAdd: async function (req, callback) {
let failed = []
let issues = []
let success = []
i = 1
await req.reduce((promise, audit) => {
// return promise.then(_ => dropbox_functions.createFolder(audit.scanner_ui)
let globalData;
return promise.then(_ => this.add(audit)
.then((data)=> {globalData = data; return dropbox_functions.createFolder(data.ui, data)}, (error)=> {failed.push({audit: audit, error: 'There was an error adding this case to the database'}); console.log(error)})
.then((data)=>{console.log(data, globalData);return dropbox_functions.checkScannerFolderExists(audit.scanner_ui)},(error)=>{issues.push({audit: globalData, error: 'There was an error creating the case folder in dropbox'})})
.then((data)=>{return dropbox_functions.moveFolder(audit.scanner_ui, globalData.ui)},(error)=>{issues.push({audit: globalData, error: 'No data folder was found so an empty one was created'}); return dropbox_functions.createDataFolder(globalData.ui)})
.then(()=>success.push({audit:globalData}), issues.push({audit: globalData, error: 'Scanner folder found but items not moved'}))
);
}, Promise.resolve()).catch(error => {console.log(error)});
return(failed, issues, success)
},
答案 0 :(得分:1)
让客户端请求等待会出现问题,是一段时间后超时还是有时显示body {
background-color: lightgreen;
}
.ion-ios-close-circle.a {
color: white;
font-size: 50px;
}
.ion-ios-close-circle.b {
background-color: white;
font-size: 50px;
}
错误。
您可以做的是
<!DOCTYPE html>
<html>
<head>
<link href="https://unpkg.com/ionicons@4.4.8/dist/css/ionicons.min.css" rel="stylesheet">
</head>
<body>
<div>
<i class="a ion-ios-close-circle"></i>
<i class="b ion-ios-close-circle"></i>
</div>
</body>
</html>
或者简单地实现no response received
或 - Make client request to server to initiate the task, and return 200OK and keep doing your task on server.
- Now write a file on server after insertion of every object as status.
- Read the file from client every 5-10 sec to check if server has completed creating objects or not.
- Mean while your task is not completed on server, show status with completion percentage or some animation.
来保持通信。