【发布时间】:2021-10-31 14:59:52
【问题描述】:
我想创建一个仅在 GCS 上复制所有文件时运行的函数。 运行 createNewTemplateFolder 函数时,“copiedFileAmout”没有更新。
这是点击功能的代码
const fileNamesToCopy = ['index.html', 'main.css','script.js','video.js','videocss.css'];
const loopFiles = async(request,response) =>{
console.log(request.body)
for (const fileNameEach of fileNamesToCopy) {
const fileName = fileNameEach;
// console.log(fileName)
createNewTemplateFolder(bucketName, originalFolder + fileName,bucketName, newFolderPath + fileName, newFolderPath);
}
response.send("Folder Created ok.")
然后循环遍历 GCS 上的文件并复制到新文件夹。
const createNewTemplateFolder = async (srcBucketName, srcFilename, destBucketName,destFileName, destFilePath) =>{
// Copies the file to the new folder
let copiedFileAmout = 0;
await storage
.bucket(srcBucketName)
.file(srcFilename)
.copy(storage.bucket(destBucketName).file(destFileName))
.then(() =>{
console.log('copy done '+ destFileName)
copyFileStatus = true;
copiedFileAmout++ /// this is not working
})
.catch((err) =>{
console.log(err)
copiedFileAmout++
})
if(copiedFileAmout === 5){
uploadCombineDelete(destFilePath) // This function should only run once when all 5 files is copied
}
在谷歌上搜索但不确定 Promise 是否无法运行增量。 希望能得到这方面的建议,谢谢!
【问题讨论】:
-
为什么不在循环之后将呼叫移至
uploadCombineDelete?这样,在所有文件都被复制后,它肯定会被调用,如果它有正确的值,你不必检查任何变量?
标签: javascript node.js google-cloud-storage google-api-nodejs-client