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

















This question has an open bounty worth +50
reputation from Ricardo Castañeda ending in 6 days.


This question has not received enough attention.




















    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

















    This question has an open bounty worth +50
    reputation from Ricardo Castañeda ending in 6 days.


    This question has not received enough attention.


















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









      marvinIsSacul

      15114




      15114










      asked 2 days ago









      Ricardo Castañeda

      2,93152137




      2,93152137






      This question has an open bounty worth +50
      reputation from Ricardo Castañeda ending in 6 days.


      This question has not received enough attention.








      This question has an open bounty worth +50
      reputation from Ricardo Castañeda ending in 6 days.


      This question has not received enough attention.


























          2 Answers
          2






          active

          oldest

          votes

















          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













            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






              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
              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 2 days ago

























                  answered 2 days ago









                  BRass

                  1,090618




                  1,090618
























                      up vote
                      0
                      down vote













                      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

























                        up vote
                        0
                        down vote













                        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























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









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









                          marvinIsSacul

                          15114




                          15114






























                               

                              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

                              Volksrepublik China

                              How to test boost logger output in unit testing?

                              Write to the output between two pipeline