Home > Mobile >  How to add user image details like date or time to file name while uploading photos to firebase stor
How to add user image details like date or time to file name while uploading photos to firebase stor

Time:09-26

I am facing a problem related to uploading images in firebase storage. I am able to upload user images in firebase storage but it is only showing the newest image. it is not showing previous images. I think it is because of the path that I have created and the timestamp in this line of code:

 const path = 'photos/$(Date.now()}.jpg';

I am using expo and React. Here is the code for uploading images:

uploadPhotoAsync = async uri => {
const path = 'photos/$(Date.now()}.jpg';

return new Promise (async(res, rej) => {
    const response = await fetch(uri)
    const file = await response.blob()
    let upload = firebase.storage().ref(path).put(file)
    upload.on("state_changed", snapshot=>{

    }, err => {
        rej(err)
    }, async ()=>{
        const url = await upload.snapshot.ref.getDownloadURL()
        res(url)
    })
})
} 

Thank you.

CodePudding user response:

There are several errors in your code:

Template literals should be delimited with backticks (`), while you delimit yours with standard single quotes ('): const path = 'photos/$(Date.now()}.jpg';.

Result: The value of path is always the same.

In addition, after the $ you use a parenthesis instead of a curly bracket.

Doing as follows shall do the trick:

const path = `photos/${Date.now()}.jpg`
  • Related