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











up vote
0
down vote

favorite












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




























    up vote
    0
    down vote

    favorite












    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


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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 16 hours ago









      marvinIsSacul

      25014




      25014










      asked Nov 16 at 14:29









      Ricardo Castañeda

      2,93152137




      2,93152137
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          +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 assigns . Hence the error - because Promise.prototype does not contain a push method.



          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
            38 mins ago


















          up vote
          0
          down vote













          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',
            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








            up vote
            1
            down vote



            +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 assigns . Hence the error - because Promise.prototype does not contain a push method.



            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
              38 mins ago















            up vote
            1
            down vote



            +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 assigns . Hence the error - because Promise.prototype does not contain a push method.



            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
              38 mins ago













            up vote
            1
            down vote



            +50







            up vote
            1
            down vote



            +50




            +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 assigns . Hence the error - because Promise.prototype does not contain a push method.



            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 assigns . Hence the error - because Promise.prototype does not contain a push method.



            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










            answered 18 hours ago









            marvinIsSacul

            25014




            25014












            • 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
              38 mins ago


















            • 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
              38 mins ago
















            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
            38 mins ago




            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
            38 mins ago












            up vote
            0
            down vote













            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



























              up vote
              0
              down vote













              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

























                up vote
                0
                down vote










                up vote
                0
                down vote









                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 at 14:59

























                answered Nov 16 at 14:50









                BRass

                1,090618




                1,090618






























                     

                    draft saved


                    draft discarded



















































                     


                    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

                    Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                    ts Property 'filter' does not exist on type '{}'

                    mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window