Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  11] [ 7]  / answers: 1 / hits: 32346  / 8 Years ago, fri, june 10, 2016, 12:00:00

When using the database you can do snapshot.exists() to check if certain data exists. According to the docs there isn't a similar method with storage.



https://firebase.google.com/docs/reference/js/firebase.storage.Reference



What is the proper way of checking if a certain file exists in Firebase Storage?


More From » firebase

 Answers
33

Firebase added an .exists() method. Another person responded and mentioned this, but the sample code they provided is incorrect. I found this thread while searching for a solution myself, and I was confused at first because I tried their code but it always was returning "File exists" even in cases when a file clearly did not exist.


exists() returns an array that contains a boolean. The correct way to use it is to check the value of the boolean, like this:


const storageFile = bucket.file('path/to/file.txt');
storageFile
.exists()
.then((exists) => {
if (exists[0]) {
console.log("File exists");
} else {
console.log("File does not exist");
}
})

I'm sharing this so the next person who finds this thread can see it and save themselves some time.


[#61818] Wednesday, June 8, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janeth

Total Points: 498
Total Questions: 91
Total Answers: 89

Location: Malaysia
Member since Wed, May 11, 2022
2 Years ago
;