How to preserve data in array from Ionic - this.array.push is not a function





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















The array storedArr = is used to store data using storage, however, I receive the .push is not a function when I try to fill it with the storage get method:



storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;


The esencial part of my code is:



import { Storage } from '@ionic/storage';

export class MyPage {
constructor(
private storage: Storage) {
}

// storedArr = ; This works but resets the array

storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;

saveToStorage() {
this.storedArr.push({ // .push is not a function
title: 'blabla',
body: 'more blabla'
});

this.storage.set('stored', this.storedArr);
}
}


How should I write that part of the code?










share|improve this question































    0















    The array storedArr = is used to store data using storage, however, I receive the .push is not a function when I try to fill it with the storage get method:



    storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;


    The esencial part of my code is:



    import { Storage } from '@ionic/storage';

    export class MyPage {
    constructor(
    private storage: Storage) {
    }

    // storedArr = ; This works but resets the array

    storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;

    saveToStorage() {
    this.storedArr.push({ // .push is not a function
    title: 'blabla',
    body: 'more blabla'
    });

    this.storage.set('stored', this.storedArr);
    }
    }


    How should I write that part of the code?










    share|improve this question



























      0












      0








      0








      The array storedArr = is used to store data using storage, however, I receive the .push is not a function when I try to fill it with the storage get method:



      storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;


      The esencial part of my code is:



      import { Storage } from '@ionic/storage';

      export class MyPage {
      constructor(
      private storage: Storage) {
      }

      // storedArr = ; This works but resets the array

      storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;

      saveToStorage() {
      this.storedArr.push({ // .push is not a function
      title: 'blabla',
      body: 'more blabla'
      });

      this.storage.set('stored', this.storedArr);
      }
      }


      How should I write that part of the code?










      share|improve this question
















      The array storedArr = is used to store data using storage, however, I receive the .push is not a function when I try to fill it with the storage get method:



      storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;


      The esencial part of my code is:



      import { Storage } from '@ionic/storage';

      export class MyPage {
      constructor(
      private storage: Storage) {
      }

      // storedArr = ; This works but resets the array

      storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ;

      saveToStorage() {
      this.storedArr.push({ // .push is not a function
      title: 'blabla',
      body: 'more blabla'
      });

      this.storage.set('stored', this.storedArr);
      }
      }


      How should I write that part of the code?







      arrays angular typescript ionic-framework storage






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 9:42









      marvinIsSacul

      53718




      53718










      asked Nov 16 '18 at 14:29









      Ricardo CastañedaRicardo Castañeda

      3,01652237




      3,01652237
























          2 Answers
          2






          active

          oldest

          votes


















          1





          +50









          The Ionic this.storage.get actually doesn't return any value other then a promise which then has to be "subscribed" to.



          So storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ; on success actually stores a promise inside storedArr then on fail it still returns a promise. Hence the error - because Promise.prototype does not contain the push method. So the ternary operator will evaluate to true and so will not be assigned to storedArr.



          In order to get the value of the Ionic this.storage.get('stored') you have to "subscribe" to the returned promise and then assign the data parameter to storedArr. Like so...



          export class MyPage {
          storedArr = ;

          constructor(private storage: Storage) {
          this.storage.get('stored')
          .then(data => {
          this.storedArr = data;
          });
          }

          saveToStorage() {
          this.storedArr.push({ // .push is not a function
          title: 'blabla',
          body: 'more blabla'
          });

          this.storage.set('stored', this.storedArr);
          }
          }





          share|improve this answer


























          • Your answer made me notice I could have written my initial code this way: storedArr = this.storage.get('stored') ? this.storage.get('stored').then(e => this.storedArr = e) : ;

            – Ricardo Castañeda
            Nov 20 '18 at 2:00






          • 1





            yea would've worked. but that's weird and it becomes that much harder to read & understand. rather move the "subscribing" to ngInit() (Angular's recommendation) or the constructor (like I showed you in the answer).

            – marvinIsSacul
            Nov 20 '18 at 4:18











          • oh I've slightly clarified the answer. please read it again (the solution remains unchanged though).

            – marvinIsSacul
            Nov 20 '18 at 4:38



















          0














          If you're storing something other than a simple primitive value, you'll likely need to do a JSON.parse() on the storage getter result. Something like below. I adjusted to use await (in place of your thens), which I think is much clearer.



          var storageResult = await this.storage.get('stored');
          storedArr = (storageResult) ? JSON.parse(storageResult) : ;


          Additionally, when you store the array you likely want to do a JSON.stringify on it.



          this.storage.set('stored', JSON.stringify(this.storedArr));





          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%2f53339804%2fhow-to-preserve-data-in-array-from-ionic-this-array-push-is-not-a-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1





            +50









            The Ionic this.storage.get actually doesn't return any value other then a promise which then has to be "subscribed" to.



            So storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ; on success actually stores a promise inside storedArr then on fail it still returns a promise. Hence the error - because Promise.prototype does not contain the push method. So the ternary operator will evaluate to true and so will not be assigned to storedArr.



            In order to get the value of the Ionic this.storage.get('stored') you have to "subscribe" to the returned promise and then assign the data parameter to storedArr. Like so...



            export class MyPage {
            storedArr = ;

            constructor(private storage: Storage) {
            this.storage.get('stored')
            .then(data => {
            this.storedArr = data;
            });
            }

            saveToStorage() {
            this.storedArr.push({ // .push is not a function
            title: 'blabla',
            body: 'more blabla'
            });

            this.storage.set('stored', this.storedArr);
            }
            }





            share|improve this answer


























            • Your answer made me notice I could have written my initial code this way: storedArr = this.storage.get('stored') ? this.storage.get('stored').then(e => this.storedArr = e) : ;

              – Ricardo Castañeda
              Nov 20 '18 at 2:00






            • 1





              yea would've worked. but that's weird and it becomes that much harder to read & understand. rather move the "subscribing" to ngInit() (Angular's recommendation) or the constructor (like I showed you in the answer).

              – marvinIsSacul
              Nov 20 '18 at 4:18











            • oh I've slightly clarified the answer. please read it again (the solution remains unchanged though).

              – marvinIsSacul
              Nov 20 '18 at 4:38
















            1





            +50









            The Ionic this.storage.get actually doesn't return any value other then a promise which then has to be "subscribed" to.



            So storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ; on success actually stores a promise inside storedArr then on fail it still returns a promise. Hence the error - because Promise.prototype does not contain the push method. So the ternary operator will evaluate to true and so will not be assigned to storedArr.



            In order to get the value of the Ionic this.storage.get('stored') you have to "subscribe" to the returned promise and then assign the data parameter to storedArr. Like so...



            export class MyPage {
            storedArr = ;

            constructor(private storage: Storage) {
            this.storage.get('stored')
            .then(data => {
            this.storedArr = data;
            });
            }

            saveToStorage() {
            this.storedArr.push({ // .push is not a function
            title: 'blabla',
            body: 'more blabla'
            });

            this.storage.set('stored', this.storedArr);
            }
            }





            share|improve this answer


























            • Your answer made me notice I could have written my initial code this way: storedArr = this.storage.get('stored') ? this.storage.get('stored').then(e => this.storedArr = e) : ;

              – Ricardo Castañeda
              Nov 20 '18 at 2:00






            • 1





              yea would've worked. but that's weird and it becomes that much harder to read & understand. rather move the "subscribing" to ngInit() (Angular's recommendation) or the constructor (like I showed you in the answer).

              – marvinIsSacul
              Nov 20 '18 at 4:18











            • oh I've slightly clarified the answer. please read it again (the solution remains unchanged though).

              – marvinIsSacul
              Nov 20 '18 at 4:38














            1





            +50







            1





            +50



            1




            +50





            The Ionic this.storage.get actually doesn't return any value other then a promise which then has to be "subscribed" to.



            So storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ; on success actually stores a promise inside storedArr then on fail it still returns a promise. Hence the error - because Promise.prototype does not contain the push method. So the ternary operator will evaluate to true and so will not be assigned to storedArr.



            In order to get the value of the Ionic this.storage.get('stored') you have to "subscribe" to the returned promise and then assign the data parameter to storedArr. Like so...



            export class MyPage {
            storedArr = ;

            constructor(private storage: Storage) {
            this.storage.get('stored')
            .then(data => {
            this.storedArr = data;
            });
            }

            saveToStorage() {
            this.storedArr.push({ // .push is not a function
            title: 'blabla',
            body: 'more blabla'
            });

            this.storage.set('stored', this.storedArr);
            }
            }





            share|improve this answer















            The Ionic this.storage.get actually doesn't return any value other then a promise which then has to be "subscribed" to.



            So storedArr = this.storage.get('stored') ? this.storage.get('stored').then((e) => {e}) : ; on success actually stores a promise inside storedArr then on fail it still returns a promise. Hence the error - because Promise.prototype does not contain the push method. So the ternary operator will evaluate to true and so will not be assigned to storedArr.



            In order to get the value of the Ionic this.storage.get('stored') you have to "subscribe" to the returned promise and then assign the data parameter to storedArr. Like so...



            export class MyPage {
            storedArr = ;

            constructor(private storage: Storage) {
            this.storage.get('stored')
            .then(data => {
            this.storedArr = data;
            });
            }

            saveToStorage() {
            this.storedArr.push({ // .push is not a function
            title: 'blabla',
            body: 'more blabla'
            });

            this.storage.set('stored', this.storedArr);
            }
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 '18 at 4:29

























            answered Nov 19 '18 at 8:13









            marvinIsSaculmarvinIsSacul

            53718




            53718













            • Your answer made me notice I could have written my initial code this way: storedArr = this.storage.get('stored') ? this.storage.get('stored').then(e => this.storedArr = e) : ;

              – Ricardo Castañeda
              Nov 20 '18 at 2:00






            • 1





              yea would've worked. but that's weird and it becomes that much harder to read & understand. rather move the "subscribing" to ngInit() (Angular's recommendation) or the constructor (like I showed you in the answer).

              – marvinIsSacul
              Nov 20 '18 at 4:18











            • oh I've slightly clarified the answer. please read it again (the solution remains unchanged though).

              – marvinIsSacul
              Nov 20 '18 at 4:38



















            • Your answer made me notice I could have written my initial code this way: storedArr = this.storage.get('stored') ? this.storage.get('stored').then(e => this.storedArr = e) : ;

              – Ricardo Castañeda
              Nov 20 '18 at 2:00






            • 1





              yea would've worked. but that's weird and it becomes that much harder to read & understand. rather move the "subscribing" to ngInit() (Angular's recommendation) or the constructor (like I showed you in the answer).

              – marvinIsSacul
              Nov 20 '18 at 4:18











            • oh I've slightly clarified the answer. please read it again (the solution remains unchanged though).

              – marvinIsSacul
              Nov 20 '18 at 4:38

















            Your answer made me notice I could have written my initial code this way: storedArr = this.storage.get('stored') ? this.storage.get('stored').then(e => this.storedArr = e) : ;

            – Ricardo Castañeda
            Nov 20 '18 at 2:00





            Your answer made me notice I could have written my initial code this way: storedArr = this.storage.get('stored') ? this.storage.get('stored').then(e => this.storedArr = e) : ;

            – Ricardo Castañeda
            Nov 20 '18 at 2:00




            1




            1





            yea would've worked. but that's weird and it becomes that much harder to read & understand. rather move the "subscribing" to ngInit() (Angular's recommendation) or the constructor (like I showed you in the answer).

            – marvinIsSacul
            Nov 20 '18 at 4:18





            yea would've worked. but that's weird and it becomes that much harder to read & understand. rather move the "subscribing" to ngInit() (Angular's recommendation) or the constructor (like I showed you in the answer).

            – marvinIsSacul
            Nov 20 '18 at 4:18













            oh I've slightly clarified the answer. please read it again (the solution remains unchanged though).

            – marvinIsSacul
            Nov 20 '18 at 4:38





            oh I've slightly clarified the answer. please read it again (the solution remains unchanged though).

            – marvinIsSacul
            Nov 20 '18 at 4:38













            0














            If you're storing something other than a simple primitive value, you'll likely need to do a JSON.parse() on the storage getter result. Something like below. I adjusted to use await (in place of your thens), which I think is much clearer.



            var storageResult = await this.storage.get('stored');
            storedArr = (storageResult) ? JSON.parse(storageResult) : ;


            Additionally, when you store the array you likely want to do a JSON.stringify on it.



            this.storage.set('stored', JSON.stringify(this.storedArr));





            share|improve this answer






























              0














              If you're storing something other than a simple primitive value, you'll likely need to do a JSON.parse() on the storage getter result. Something like below. I adjusted to use await (in place of your thens), which I think is much clearer.



              var storageResult = await this.storage.get('stored');
              storedArr = (storageResult) ? JSON.parse(storageResult) : ;


              Additionally, when you store the array you likely want to do a JSON.stringify on it.



              this.storage.set('stored', JSON.stringify(this.storedArr));





              share|improve this answer




























                0












                0








                0







                If you're storing something other than a simple primitive value, you'll likely need to do a JSON.parse() on the storage getter result. Something like below. I adjusted to use await (in place of your thens), which I think is much clearer.



                var storageResult = await this.storage.get('stored');
                storedArr = (storageResult) ? JSON.parse(storageResult) : ;


                Additionally, when you store the array you likely want to do a JSON.stringify on it.



                this.storage.set('stored', JSON.stringify(this.storedArr));





                share|improve this answer















                If you're storing something other than a simple primitive value, you'll likely need to do a JSON.parse() on the storage getter result. Something like below. I adjusted to use await (in place of your thens), which I think is much clearer.



                var storageResult = await this.storage.get('stored');
                storedArr = (storageResult) ? JSON.parse(storageResult) : ;


                Additionally, when you store the array you likely want to do a JSON.stringify on it.



                this.storage.set('stored', JSON.stringify(this.storedArr));






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 16 '18 at 14:59

























                answered Nov 16 '18 at 14:50









                BRassBRass

                1,453725




                1,453725






























                    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%2f53339804%2fhow-to-preserve-data-in-array-from-ionic-this-array-push-is-not-a-function%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

                    Bressuire

                    Vorschmack

                    Quarantine