The put
method returns a promise, so you can wait for that to resolve instead of passing in the callbacks. When doing that, the code to upload the file and then store its download URL in the database becomes:
function uploadFile(file) {
const filename = emailValue + Date.now();
storage.ref("arts/" + filename).put(file.files[0]).then(() => {
task.snapshot.ref.getDownloadURL().then((downloadURL) => {
db.collection("participants").doc(emailfilename).set({
email: emailValue,
url: downloadURL.toString()
})
});
}
);
}
Note there are quite some subtle changes in the above code, so:
- There may be syntax errors as I didn't run this, so treat it as pseudo-code. If you get an error, first try to solve it yourself please - and if you do, post a comment or edit to the answer with the fix.
- If you also need progress reporting, you can listen for
state_changed
. Just ignore the completed
event as that is handled by then()
in the code above.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…