1 回答
TA贡献1831条经验 获得超10个赞
该getDownloadURL()方法是异步的,并返回一个使用 downloadURL 解析的 Promise。
因此,您需要按照这些方式做一些事情,使用then()方法等待 Promise 解决:
uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
console.log('File available at', downloadURL);
});
在您的代码中不清楚这两个不同部分是如何链接的(哪个调用哪个以及如何选择文件),但下面显示了一种可能性:
let downloadURL;
var filename = filenamechosen;
var storageRef = firebase.storage().ref('/dav' + 'projects' + '/' + filename);
var uploadTask = storageRef.put(selectedFile);
uploadTask.on("state_changed",
null, /// <- See https://firebase.google.com/docs/reference/js/firebase.storage.UploadTask#on
null,
function(){
// Upload completed successfully, now we can get the download URL
uploadTask.snapshot.ref.getDownloadURL()
.then(function(downloadURL) {
console.log('File available at', downloadURL);
uploadOne(downloadURL); // <- We call the uploadOne function passing the downloadURL as parameter
});
});
function uploadOne(downloadURL){
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(function() {
console.log("done");
})
.catch(function(error) {
console.log(error);
});
}
这种方式是uploadTask触发对数据库的写入。您可以根据需要进行调整,但在任何情况下,您都只会downloadURL在 Promise 实现时调用的回调函数中获得 的值。
添加回答
举报