Javascript 发送的响应延迟了1个响应
我从Node.js服务器收到的响应总是延迟1个响应。我会一直收到我按下上传按钮时应该收到的回复 节点服务器JU启动时,响应为空。在过去的四个小时里,我尝试了无数的事情,但我想不出来 我从bodyParser切换到一个名为express busboy的库来上传文件,因为我不必重新编写整个代码。我的应用程序中的所有其他功能都可以正常工作,因此我开始认为问题可能与库的文件上载部分有关 这是我的节点代码,抱歉,读起来可能有点乏味,但我真的迷路了,这是唯一阻止我前进的东西 这是我后端的代码Javascript 发送的响应延迟了1个响应,javascript,node.js,angular,Javascript,Node.js,Angular,我从Node.js服务器收到的响应总是延迟1个响应。我会一直收到我按下上传按钮时应该收到的回复 节点服务器JU启动时,响应为空。在过去的四个小时里,我尝试了无数的事情,但我想不出来 我从bodyParser切换到一个名为express busboy的库来上传文件,因为我不必重新编写整个代码。我的应用程序中的所有其他功能都可以正常工作,因此我开始认为问题可能与库的文件上载部分有关 这是我的节点代码,抱歉,读起来可能有点乏味,但我真的迷路了,这是唯一阻止我前进的东西 这是我后端的代码 import
import * as fse from 'fs-extra';
export default class PhotoController{
private postError = [];
private postSuccess = [];
public postPhotos = (req, res) => {
const fileArray = Array.isArray(req.files.file) ? req.files.file : new Array(req.files.file);
const body = new Array(req.body);
const item = body[0].item;
const staticFileDest = `C:\\Users\\JP\\Desktop\\MiMApp\\MiMBackEnd\\movedUploads`;
for (let i = 0; i < fileArray.length; i++){
const removeDest = `./uploads/${fileArray[i].uuid}`;
const newName = Array.isArray(body[0].newName)? body[0].newName[i] : body[0].newName;
const srcFile = `./${fileArray[i].file}`;
const moveDest = `${staticFileDest}\\${item}\\${newName}`;
fse.pathExists(moveDest)
.then(exists => {
if (exists){
this.postError.push(`Fichier ${newName} existe deja.`);
this.removeSrcFile(removeDest);
} else {
fse.move(srcFile, moveDest)
.then( () => {
const commentToAdd = Array.isArray(body[0].comment) ? body[0].comment[i] : body[0].comment;
const commentPath = moveDest.replace(/\.[^/.]+$/, "").concat('.txt');
this.postSuccess.push(`Fichier ${newName} à été ajouté dans l'item : ${item}`);
this.removeSrcFile(removeDest);
fse.writeFile(commentPath, commentToAdd)
.then(() => {this.postSuccess.push(`Le commentaire à été ajouté pour ${newName}`)})
.catch(() => {this.postError.push(`Le commentaire pour ${newName} existe deja.`)});
})
.catch(() => {
this.postError.push(`Le fichier ${newName} n'a pas été rajouté correctement.`);
this.removeSrcFile(removeDest);
this.removeSrcFile(moveDest);
});
}
});
}
this.sendResponse(res);
this.resetErrors();
};
private removeSrcFile(file){
fse.remove(file)
.then(() => {})
.catch(() => {this.postError.push(`${file} n'a pas pu être éffacé.`)});
}
private resetErrors(){
this.postError = [];
this.postSuccess = [];
}
private sendResponse(res){
if (this.postError.length > 0){
res.send({success : this.postSuccess, errors : this.postError});
} else {
res.send({success: this.postSuccess});
}
}
}
服务:
export class PhotoService {
constructor(private http: HttpClient, private config: Config) {}
public postPictures = (datas): Observable<any> => {
return this.http.post(`${this.config.apiAddress}/postPictures`, datas)
.pipe(
map(data => {return data})
);
}
}
如果我再次提交:
{"success":["Fichier 15012019-YEDwo.jpg à été ajouté dans l'item : 55","Le commentaire à été ajouté pour 15012019-YEDwo.jpg"]}
您执行一个asynchrounus函数,但您的发送函数
this.sendResponse(res)代码>被同步触发
fse.move(srcFile, moveDest)
.then( () => {
...
}
这意味着当fse.move
正在运行时,您已经发送了请求,而此.postsucture
只是在此处初始化的空数组private postsucture=[]代码>。第二次fse.move
完成,这就是为什么您的回复中充满了数据。非常感谢您抽出时间。我认为For循环之外的其余代码只有在它完成了对内部发生的事情的无规则处理之后才能执行。现在开始工作了,不客气。每次看到.then()(或使用rxjs时的subscribe())时,这意味着该函数将被称为asynchrounus,并且每次同步函数都会更快。部分正确,for循环已经完成,但是已经创建了多个异步函数,当它们完成时,这些函数将执行它们的回调,并且回调将填充数组,而不是for循环。祝你现在编码愉快。
{"success":["Fichier 15012019-YEDwo.jpg à été ajouté dans l'item : 55","Le commentaire à été ajouté pour 15012019-YEDwo.jpg"]}
fse.move(srcFile, moveDest)
.then( () => {
...
}