获取下载 URL(Firebase 存储)并将其保存在 Firebase 实时数据库中

我一直在尝试制作一些东西,它只是将一个项目上传到 Firebase 等等。我无法真正解释这个项目,但我想要做的是:

  1. 上传文件时获取 Firebase 下载 URL。

  2. 将其保存在 Firebase 实时数据库中。

我为它制作的代码是:

  let downloadURL;

    var filename = filenamechosen;

    var storageRef = firebase.storage().ref('/dav' + 'projects' + '/' + filename);

    var uploadTask = storageRef.put(selectedFile);


    uploadTask.on("state_changed", function(snapshot){


    }, function(error){


    }, function(){

        console.log(uploadTask.snapshot.ref.getDownloadURL());

    });

    };


function uploadOne(){

    let projectinf = document.getElementById("projectinfo").value;

    let name = document.getElementById("studentname").value;

    let cls = document.getElementById("cls").value;

    let email = document.getElementById("email").value;

    let projectlnk = downloadURL;

    let marks = document.getElementById("marks").value;

    let submitfrm = document.getElementById("submitfrm");

    let studentObj = {

        class: cls,

        email: email, 

        projectinfo: projectinf,

        projectlink: projectlnk,

        marks: "lol",

    }

    firebase.database().ref('/schools/dav/').child(`${name}`).set(studentObj).then().catch();

    console.log("done");

}

为了获取下载 url,我收到了这样的回复:


jt {a: 0, i: undefined, c: jt, b: null, f: null, …}

a: 2

b: null

c: null

f: null

g: false

h: false

i: "https://firebasestorage.googleapis.com/v0/b/workspace-c7042.appspot.com/o/davproject.... //Continues

我不知道如何在获取下载 URL 后将其存储在 Firebase 实时数据库中:


<form action="uploadOne()">

<!--Some inputs as defined in the uploadOne function.-->

<button type="submit">SUBMIT</button>

</form>

这几乎是我与那件事一起使用的整个形式。


白猪掌柜的
浏览 126回答 1
1回答

慕哥6287543

该getDownloadURL()方法是异步的,并返回一个使用 downloadURL 解析的 Promise。因此,您需要按照这些方式做一些事情,使用then()方法等待 Promise 解决:&nbsp; uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {&nbsp; &nbsp; console.log('File available at', downloadURL);&nbsp; });在您的代码中不清楚这两个不同部分是如何链接的(哪个调用哪个以及如何选择文件),但下面显示了一种可能性:let downloadURL;var filename = filenamechosen;var storageRef = firebase.storage().ref('/dav' + 'projects' + '/' + filename);var uploadTask = storageRef.put(selectedFile);uploadTask.on("state_changed",null,&nbsp; /// <- See https://firebase.google.com/docs/reference/js/firebase.storage.UploadTask#onnull,function(){&nbsp; &nbsp;// Upload completed successfully, now we can get the download URL&nbsp; &nbsp; uploadTask.snapshot.ref.getDownloadURL()&nbsp; &nbsp; .then(function(downloadURL) {&nbsp; &nbsp; &nbsp; &nbsp; console.log('File available at', downloadURL);&nbsp; &nbsp; &nbsp; &nbsp; uploadOne(downloadURL);&nbsp; // <- We call the uploadOne function passing the downloadURL as parameter&nbsp; &nbsp; });});function uploadOne(downloadURL){&nbsp; &nbsp; let projectinf = document.getElementById("projectinfo").value;&nbsp; &nbsp; let name = document.getElementById("studentname").value;&nbsp; &nbsp; let cls = document.getElementById("cls").value;&nbsp; &nbsp; let email = document.getElementById("email").value;&nbsp; &nbsp; let projectlnk = downloadURL;&nbsp; &nbsp; let marks = document.getElementById("marks").value;&nbsp; &nbsp; let submitfrm = document.getElementById("submitfrm");&nbsp; &nbsp; let studentObj = {&nbsp; &nbsp; &nbsp; &nbsp; class: cls,&nbsp; &nbsp; &nbsp; &nbsp; email: email,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; projectinfo: projectinf,&nbsp; &nbsp; &nbsp; &nbsp; projectlink: projectlnk,&nbsp; &nbsp; &nbsp; &nbsp; marks: "lol",&nbsp; &nbsp; }&nbsp; &nbsp; firebase.database().ref('/schools/dav/').child(`${name}`).set(studentObj)&nbsp; &nbsp; .then(function() {&nbsp; &nbsp; &nbsp; &nbsp; console.log("done");&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; .catch(function(error) {&nbsp; &nbsp; &nbsp; &nbsp; console.log(error);&nbsp; &nbsp; &nbsp; });}这种方式是uploadTask触发对数据库的写入。您可以根据需要进行调整,但在任何情况下,您都只会downloadURL在 Promise 实现时调用的回调函数中获得 的值。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript