node.js - 在 firebase 云函数中,bucket.upload promise 解决得太早

标签 node.js firebase firebase-storage es6-promise google-cloud-functions

我写了一个这样工作的函数

onNewZipFileRequested
{get all the necessary data}
.then{download all the files}
.then{create a zipfile with all those file}
.then{upload that zipfile} (*here is the problem)
.than{update the database  with the signedUrl of the file}

这里是相关代码

[***CREATION OF ZIP FILE WORKING****]
}).then(() =>{
    zip.generateNodeStream({type:'nodebuffer',streamFiles:true})
    .pipe(fs.createWriteStream(tempPath))
    .on('finish', function () {
        console.log("zip written.");
        return bucket.upload(tempPath, {    //**** problem****
            destination: destinazionePath
        });
    });
}).then(()=>{
    const config = {
        action:'read',
        expires:'03-09-2391'
    }
    return bucket.file(destinazionePath).getSignedUrl(config)
}).then(risultato=>{
    const daSalvare ={
        signedUrl: risultato[0],
        status : 'fatto',
        dataInserimento : zipball.dataInserimento
    }
    return event.data.ref.set(daSalvare)
})

在客户端,一旦应用程序看到状态变化和新的 Url,就会出现一个下载按钮(指向新的 url)

一切正常,但如果我尝试立即下载文件...还没有文件!!!

如果我同时等待并重试,文件就在那里。

我注意到我必须等待的时间取决于 zip 文件的大小。

bucket.upload promise 应该在上传结束时解析,但显然触发得太早了。 有没有办法确切知道文件何时准备好? 我可能必须制作同样非常大的文件,如果这个过程需要几分钟,这不是问题,但我需要知道它何时结束。

* 编辑 *

代码中存在不必要的嵌套。虽然这不是错误(重构前后结果相同),但它在答案中造成了一些困惑,所以我将其编辑掉了。

Id' 想指出我仅在获得签名 url 后才更新数据库,并且我仅在上传后才获得它(否则我无法做到),因此要在所有 promise 链中获得任何结果都必须工作,并且事实上它确实如此。当在客户端出现下载按钮时(当“状态”变为“fatto”时发生)它已经链接到正确的签名 url,但如果我过早按下它,文件不存在(失败 - 无文件)。如果我等一秒钟(文件越大,我等待的时间越长),那么文件就在那里。

(英语不是我的母语,如果我不清楚请问我会尽力解释自己)

最佳答案

看起来问题可能是大括号没有正确对齐,导致 then 语句嵌入到另一个语句中。以下是 then 语句分隔的代码:

[***CREATION OF ZIP FILE WORKING****]}).then(() => {
    zip.generateNodeStream({type: 'nodebuffer', streamFiles: true})
    .pipe(fs.createWriteStream(tempPath))
    .on('finish', function () {
        console.log('zip written.')
        return bucket.upload(tempPath, { 
            destination: destinazionePath
        })
    })
}).then(() => {
    const config = {
        action: 'read',
        expires: '03-09-2391'
    }
    return bucket.file(destinazionePath).getSignedUrl(config)
}).then(risultato => {
        const daSalvare = {
            signedUrl: risultato[0],
            status : 'fatto',
            dataInserimento : zipball.dataInserimento
    }
    return event.data.ref.set(daSalvare)
})

关于node.js - 在 firebase 云函数中,bucket.upload promise 解决得太早,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45007091/

相关文章:

javascript - Node/Expressjs 'No Access-Control-Allow-Origin header is present' Twitter API

android - Android-设置Google Play服务和Firebase-Gradle构建错误

node.js - NodeJS 服务器端 Firebase 存储的安全外部链接

上传到存储时 Firebase Uncaught [object Object]

python - 安装Appium的一些问题

javascript - 将授权 header 添加到 GET 请求

node.js - 无法使用 npm rundevelop 命令运行 Gatsby 应用程序

java - 适配器不显示 Firebase 实时数据库中的产品图片和价格

javascript - Firebase Storage 下载到电脑

javascript - 无法将文件上传到我的 Firebase 存储 - 错误 403 Forbidden