Issue when trying to create AngularFireStorageReference using variable










0















When creating a storage reference using example code below, I have no issue. However, when I replace "1234" with this.id I get the below issue in screenshot. How do I fix this? All documentation I have read online indicates that this should be working.



export class NewListComponent implements OnInit 

uploadPercent: Observable<number>;
downloadURL: Observable<string>;
uploadProgress: Observable<number>;
ref: AngularFireStorageReference;
task: AngularFireUploadTask;
uploadState: Observable<string>;
id:string;
imgObsArray: Observable<string> = new Array();

constructor(private afStorage: AngularFireStorage)



ngOnInit()



uploadFile(event)

this.id = Math.random().toString(36).substring(2);
this.ref = this.afStorage.ref("1234");
this.task = this.ref.put(event.target.files[0]);
this.uploadState = this.task.snapshotChanges().pipe(map(s => s.state));
this.uploadProgress = this.task.percentageChanges();

this.ref.getDownloadURL()
.subscribe(avatarUrl =>
this.downloadURL = avatarUrl
this.imgObsArray.push(this.downloadURL)
console.log(avatarUrl + " This is the avatarUrl " + this.downloadURL + " This is the downloadURL ");
, (error) =>
console.error(error);
);






Error



enter image description here



UPDATED CODE BASED ON FEEDBACK



uploadFile(event) 
const id = Math.random().toString(36).substring(2);

console.log(id + " THIS IS THE RANDOM ID")

this.ref = this.afStorage.ref(id);

this.task = this.ref.put(event.target.files[0]);

this.uploadState = this.task.snapshotChanges().pipe(map(s => s.state));

this.uploadProgress = this.task.percentageChanges();

this.task.snapshotChanges().pipe(
finalize(() => this.downloadURL = this.ref.getDownloadURL())).subscribe();

this.downloadURL.pipe(
filter( url => url !== undefined),
tap(avatarUrl => this.imgObsArray.push(avatarUrl),
console.log(avatarUrl + " This is the avatarUrl " + avatarUrl + " This is the downloadURL "); ),
catchError( error =>
console.error(error);
return Observable.throw(error);
)).subscribe();




enter image description here










share|improve this question
























  • does your code work if "x8d2nlj7yrl" instead of "1234"? If it does, then there could be problem with toString(radix: number) function.

    – KiraAG
    Nov 16 '18 at 4:59












  • @KiraAG using "x8d2nlj7yrl" instead of "1234" my code still works.

    – Rogerto
    Nov 16 '18 at 5:18











  • ok. So problem might be because of this reference of TS. Either try const id = Math.random().toString(36).substring(2); or this.afStorage.ref(Math.random().toString(36).substring(2));

    – KiraAG
    Nov 16 '18 at 5:32











  • This is 404 error and its coming from server . Error message is pretty clear that the id you are passing is invalid.

    – Sunil Singh
    Nov 16 '18 at 21:56











  • Thank you @KiraAG, I have debugged further and I think the problem is actually that given I am uploading the file and then trying to get the download URL in the same function and it is happening asynchronously, it is returning a 404 as the file is yet to be uploaded. The reason I think is because is that first try returns a 404, second try, third try etc. all works fine. Is there a way to fix this???

    – Rogerto
    Nov 19 '18 at 1:13















0















When creating a storage reference using example code below, I have no issue. However, when I replace "1234" with this.id I get the below issue in screenshot. How do I fix this? All documentation I have read online indicates that this should be working.



export class NewListComponent implements OnInit 

uploadPercent: Observable<number>;
downloadURL: Observable<string>;
uploadProgress: Observable<number>;
ref: AngularFireStorageReference;
task: AngularFireUploadTask;
uploadState: Observable<string>;
id:string;
imgObsArray: Observable<string> = new Array();

constructor(private afStorage: AngularFireStorage)



ngOnInit()



uploadFile(event)

this.id = Math.random().toString(36).substring(2);
this.ref = this.afStorage.ref("1234");
this.task = this.ref.put(event.target.files[0]);
this.uploadState = this.task.snapshotChanges().pipe(map(s => s.state));
this.uploadProgress = this.task.percentageChanges();

this.ref.getDownloadURL()
.subscribe(avatarUrl =>
this.downloadURL = avatarUrl
this.imgObsArray.push(this.downloadURL)
console.log(avatarUrl + " This is the avatarUrl " + this.downloadURL + " This is the downloadURL ");
, (error) =>
console.error(error);
);






Error



enter image description here



UPDATED CODE BASED ON FEEDBACK



uploadFile(event) 
const id = Math.random().toString(36).substring(2);

console.log(id + " THIS IS THE RANDOM ID")

this.ref = this.afStorage.ref(id);

this.task = this.ref.put(event.target.files[0]);

this.uploadState = this.task.snapshotChanges().pipe(map(s => s.state));

this.uploadProgress = this.task.percentageChanges();

this.task.snapshotChanges().pipe(
finalize(() => this.downloadURL = this.ref.getDownloadURL())).subscribe();

this.downloadURL.pipe(
filter( url => url !== undefined),
tap(avatarUrl => this.imgObsArray.push(avatarUrl),
console.log(avatarUrl + " This is the avatarUrl " + avatarUrl + " This is the downloadURL "); ),
catchError( error =>
console.error(error);
return Observable.throw(error);
)).subscribe();




enter image description here










share|improve this question
























  • does your code work if "x8d2nlj7yrl" instead of "1234"? If it does, then there could be problem with toString(radix: number) function.

    – KiraAG
    Nov 16 '18 at 4:59












  • @KiraAG using "x8d2nlj7yrl" instead of "1234" my code still works.

    – Rogerto
    Nov 16 '18 at 5:18











  • ok. So problem might be because of this reference of TS. Either try const id = Math.random().toString(36).substring(2); or this.afStorage.ref(Math.random().toString(36).substring(2));

    – KiraAG
    Nov 16 '18 at 5:32











  • This is 404 error and its coming from server . Error message is pretty clear that the id you are passing is invalid.

    – Sunil Singh
    Nov 16 '18 at 21:56











  • Thank you @KiraAG, I have debugged further and I think the problem is actually that given I am uploading the file and then trying to get the download URL in the same function and it is happening asynchronously, it is returning a 404 as the file is yet to be uploaded. The reason I think is because is that first try returns a 404, second try, third try etc. all works fine. Is there a way to fix this???

    – Rogerto
    Nov 19 '18 at 1:13













0












0








0








When creating a storage reference using example code below, I have no issue. However, when I replace "1234" with this.id I get the below issue in screenshot. How do I fix this? All documentation I have read online indicates that this should be working.



export class NewListComponent implements OnInit 

uploadPercent: Observable<number>;
downloadURL: Observable<string>;
uploadProgress: Observable<number>;
ref: AngularFireStorageReference;
task: AngularFireUploadTask;
uploadState: Observable<string>;
id:string;
imgObsArray: Observable<string> = new Array();

constructor(private afStorage: AngularFireStorage)



ngOnInit()



uploadFile(event)

this.id = Math.random().toString(36).substring(2);
this.ref = this.afStorage.ref("1234");
this.task = this.ref.put(event.target.files[0]);
this.uploadState = this.task.snapshotChanges().pipe(map(s => s.state));
this.uploadProgress = this.task.percentageChanges();

this.ref.getDownloadURL()
.subscribe(avatarUrl =>
this.downloadURL = avatarUrl
this.imgObsArray.push(this.downloadURL)
console.log(avatarUrl + " This is the avatarUrl " + this.downloadURL + " This is the downloadURL ");
, (error) =>
console.error(error);
);






Error



enter image description here



UPDATED CODE BASED ON FEEDBACK



uploadFile(event) 
const id = Math.random().toString(36).substring(2);

console.log(id + " THIS IS THE RANDOM ID")

this.ref = this.afStorage.ref(id);

this.task = this.ref.put(event.target.files[0]);

this.uploadState = this.task.snapshotChanges().pipe(map(s => s.state));

this.uploadProgress = this.task.percentageChanges();

this.task.snapshotChanges().pipe(
finalize(() => this.downloadURL = this.ref.getDownloadURL())).subscribe();

this.downloadURL.pipe(
filter( url => url !== undefined),
tap(avatarUrl => this.imgObsArray.push(avatarUrl),
console.log(avatarUrl + " This is the avatarUrl " + avatarUrl + " This is the downloadURL "); ),
catchError( error =>
console.error(error);
return Observable.throw(error);
)).subscribe();




enter image description here










share|improve this question
















When creating a storage reference using example code below, I have no issue. However, when I replace "1234" with this.id I get the below issue in screenshot. How do I fix this? All documentation I have read online indicates that this should be working.



export class NewListComponent implements OnInit 

uploadPercent: Observable<number>;
downloadURL: Observable<string>;
uploadProgress: Observable<number>;
ref: AngularFireStorageReference;
task: AngularFireUploadTask;
uploadState: Observable<string>;
id:string;
imgObsArray: Observable<string> = new Array();

constructor(private afStorage: AngularFireStorage)



ngOnInit()



uploadFile(event)

this.id = Math.random().toString(36).substring(2);
this.ref = this.afStorage.ref("1234");
this.task = this.ref.put(event.target.files[0]);
this.uploadState = this.task.snapshotChanges().pipe(map(s => s.state));
this.uploadProgress = this.task.percentageChanges();

this.ref.getDownloadURL()
.subscribe(avatarUrl =>
this.downloadURL = avatarUrl
this.imgObsArray.push(this.downloadURL)
console.log(avatarUrl + " This is the avatarUrl " + this.downloadURL + " This is the downloadURL ");
, (error) =>
console.error(error);
);






Error



enter image description here



UPDATED CODE BASED ON FEEDBACK



uploadFile(event) 
const id = Math.random().toString(36).substring(2);

console.log(id + " THIS IS THE RANDOM ID")

this.ref = this.afStorage.ref(id);

this.task = this.ref.put(event.target.files[0]);

this.uploadState = this.task.snapshotChanges().pipe(map(s => s.state));

this.uploadProgress = this.task.percentageChanges();

this.task.snapshotChanges().pipe(
finalize(() => this.downloadURL = this.ref.getDownloadURL())).subscribe();

this.downloadURL.pipe(
filter( url => url !== undefined),
tap(avatarUrl => this.imgObsArray.push(avatarUrl),
console.log(avatarUrl + " This is the avatarUrl " + avatarUrl + " This is the downloadURL "); ),
catchError( error =>
console.error(error);
return Observable.throw(error);
)).subscribe();




enter image description here







angular firebase google-cloud-firestore firebase-storage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 5:20







Rogerto

















asked Nov 16 '18 at 2:36









RogertoRogerto

67110




67110












  • does your code work if "x8d2nlj7yrl" instead of "1234"? If it does, then there could be problem with toString(radix: number) function.

    – KiraAG
    Nov 16 '18 at 4:59












  • @KiraAG using "x8d2nlj7yrl" instead of "1234" my code still works.

    – Rogerto
    Nov 16 '18 at 5:18











  • ok. So problem might be because of this reference of TS. Either try const id = Math.random().toString(36).substring(2); or this.afStorage.ref(Math.random().toString(36).substring(2));

    – KiraAG
    Nov 16 '18 at 5:32











  • This is 404 error and its coming from server . Error message is pretty clear that the id you are passing is invalid.

    – Sunil Singh
    Nov 16 '18 at 21:56











  • Thank you @KiraAG, I have debugged further and I think the problem is actually that given I am uploading the file and then trying to get the download URL in the same function and it is happening asynchronously, it is returning a 404 as the file is yet to be uploaded. The reason I think is because is that first try returns a 404, second try, third try etc. all works fine. Is there a way to fix this???

    – Rogerto
    Nov 19 '18 at 1:13

















  • does your code work if "x8d2nlj7yrl" instead of "1234"? If it does, then there could be problem with toString(radix: number) function.

    – KiraAG
    Nov 16 '18 at 4:59












  • @KiraAG using "x8d2nlj7yrl" instead of "1234" my code still works.

    – Rogerto
    Nov 16 '18 at 5:18











  • ok. So problem might be because of this reference of TS. Either try const id = Math.random().toString(36).substring(2); or this.afStorage.ref(Math.random().toString(36).substring(2));

    – KiraAG
    Nov 16 '18 at 5:32











  • This is 404 error and its coming from server . Error message is pretty clear that the id you are passing is invalid.

    – Sunil Singh
    Nov 16 '18 at 21:56











  • Thank you @KiraAG, I have debugged further and I think the problem is actually that given I am uploading the file and then trying to get the download URL in the same function and it is happening asynchronously, it is returning a 404 as the file is yet to be uploaded. The reason I think is because is that first try returns a 404, second try, third try etc. all works fine. Is there a way to fix this???

    – Rogerto
    Nov 19 '18 at 1:13
















does your code work if "x8d2nlj7yrl" instead of "1234"? If it does, then there could be problem with toString(radix: number) function.

– KiraAG
Nov 16 '18 at 4:59






does your code work if "x8d2nlj7yrl" instead of "1234"? If it does, then there could be problem with toString(radix: number) function.

– KiraAG
Nov 16 '18 at 4:59














@KiraAG using "x8d2nlj7yrl" instead of "1234" my code still works.

– Rogerto
Nov 16 '18 at 5:18





@KiraAG using "x8d2nlj7yrl" instead of "1234" my code still works.

– Rogerto
Nov 16 '18 at 5:18













ok. So problem might be because of this reference of TS. Either try const id = Math.random().toString(36).substring(2); or this.afStorage.ref(Math.random().toString(36).substring(2));

– KiraAG
Nov 16 '18 at 5:32





ok. So problem might be because of this reference of TS. Either try const id = Math.random().toString(36).substring(2); or this.afStorage.ref(Math.random().toString(36).substring(2));

– KiraAG
Nov 16 '18 at 5:32













This is 404 error and its coming from server . Error message is pretty clear that the id you are passing is invalid.

– Sunil Singh
Nov 16 '18 at 21:56





This is 404 error and its coming from server . Error message is pretty clear that the id you are passing is invalid.

– Sunil Singh
Nov 16 '18 at 21:56













Thank you @KiraAG, I have debugged further and I think the problem is actually that given I am uploading the file and then trying to get the download URL in the same function and it is happening asynchronously, it is returning a 404 as the file is yet to be uploaded. The reason I think is because is that first try returns a 404, second try, third try etc. all works fine. Is there a way to fix this???

– Rogerto
Nov 19 '18 at 1:13





Thank you @KiraAG, I have debugged further and I think the problem is actually that given I am uploading the file and then trying to get the download URL in the same function and it is happening asynchronously, it is returning a 404 as the file is yet to be uploaded. The reason I think is because is that first try returns a 404, second try, third try etc. all works fine. Is there a way to fix this???

– Rogerto
Nov 19 '18 at 1:13












1 Answer
1






active

oldest

votes


















0














Adding as an answer, You should retrieve the downloadUrl on the finalize of snapshot changes as mentioned in storage docs. To resolve the pipe error, its better to move this.downloadUrl subscription to ngOnInit.



PS: After further discussion , it seems that ref.getDownloadUrl can initially return null.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330641%2fissue-when-trying-to-create-angularfirestoragereference-using-variable%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Adding as an answer, You should retrieve the downloadUrl on the finalize of snapshot changes as mentioned in storage docs. To resolve the pipe error, its better to move this.downloadUrl subscription to ngOnInit.



    PS: After further discussion , it seems that ref.getDownloadUrl can initially return null.






    share|improve this answer



























      0














      Adding as an answer, You should retrieve the downloadUrl on the finalize of snapshot changes as mentioned in storage docs. To resolve the pipe error, its better to move this.downloadUrl subscription to ngOnInit.



      PS: After further discussion , it seems that ref.getDownloadUrl can initially return null.






      share|improve this answer

























        0












        0








        0







        Adding as an answer, You should retrieve the downloadUrl on the finalize of snapshot changes as mentioned in storage docs. To resolve the pipe error, its better to move this.downloadUrl subscription to ngOnInit.



        PS: After further discussion , it seems that ref.getDownloadUrl can initially return null.






        share|improve this answer













        Adding as an answer, You should retrieve the downloadUrl on the finalize of snapshot changes as mentioned in storage docs. To resolve the pipe error, its better to move this.downloadUrl subscription to ngOnInit.



        PS: After further discussion , it seems that ref.getDownloadUrl can initially return null.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 5:50









        KiraAGKiraAG

        401311




        401311





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330641%2fissue-when-trying-to-create-angularfirestoragereference-using-variable%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党