使用 gcs API 时,JS 增量未在 for 循环中更新
JS Increment not updating in for loop of while using gcs API
我想创建一个函数,仅当所有文件都复制到 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
}
在 google 上搜索但不确定 Promise 是否无法 运行 增量。
希望能得到这方面的建议,谢谢!
您必须在函数外部(全局范围)声明变量 copiedFileAmout。
let copiedFileAmout = 0;
const fileNamesToCopy = ['index.html', 'main.css','script.js','video.js','videocss.css'];
const loopFiles = async(request,response) =>{
....
....
然后删除行
let copiedFileAmout = 0;
来自函数 createNewTemplateFolder
您有 2 个选择:
在函数之外声明 copiedFileAmount,这样两者都可以访问它
let copiedFileAmount = 0;
或者一个不太有利的方法,只需将当前变量的声明从 let 更改为 var,这样就可以提供更大的范围。
var copiedFileAmount = 0;
我想创建一个函数,仅当所有文件都复制到 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
}
在 google 上搜索但不确定 Promise 是否无法 运行 增量。 希望能得到这方面的建议,谢谢!
您必须在函数外部(全局范围)声明变量 copiedFileAmout。
let copiedFileAmout = 0;
const fileNamesToCopy = ['index.html', 'main.css','script.js','video.js','videocss.css'];
const loopFiles = async(request,response) =>{
....
....
然后删除行
let copiedFileAmout = 0;
来自函数 createNewTemplateFolder
您有 2 个选择:
在函数之外声明 copiedFileAmount,这样两者都可以访问它
let copiedFileAmount = 0;
或者一个不太有利的方法,只需将当前变量的声明从 let 更改为 var,这样就可以提供更大的范围。
var copiedFileAmount = 0;