Ionic 3 storage set and get acting weird





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







0















I have an Ionic 3 app, where I store an array of objects in SQLite.



My this.data array (explained in the lower section) looks like this:



[
{
guid: "xy",
images: [
{ guid: 0, uploaded: true },
{ guid: 1, uploaded: true }
],
},
{
guid: "xz",
images: [
{ guid: 0, uploaded: false },
{ guid: 1, uploaded: false }
],
}
]


So an object has a guid and an array of objects again. After an item is updated I want to save all the items to the storage as well as to the uploader class' variable. The uploader class has a this.data and this.key property.



Here is a snippet from the problematic part:



updateItem(value){
// search for the index of the item in the array
var index = this.data.findIndex((item) => {
return item.guid == value.guid;
});

if(index >= 0){
// update the item in the array with the changed one
this.data[index] = value;

// this works fine, prints with the updated item
console.log(this.data);

// it supposed to save the whole array with the changed items
return this.storage.set(this.key, this.data).then(() => {

// for debug I read the data again - same happens after app restart
this.storage.get(this.key).then((data) => {
// this logs the initial, unchanged items
console.log(data);
});
});
}

return Promise.reject(new Error('error'));
}


First, it searches for the index of the item in the this.data array, if found, then overrides the item in the array.
Then it tries to save it into the storage.
For debugging purposes, I read the storage and console.log it.



After I set the "xz" object's images to uploaded = true, I call the updateItem(secondItem).



From the first console.log I see that "xy" and "xy" object's images are all uploaded: true. The storage.set called, and inside the storage.get, the initial state occurs. The "xy" object's images are uploaded: true, but the "xz" object's are false.
After I restart my app, this state loads again.



If there is only one object in this.data, the updateItem works fine, for example, I set the storage with uploaded: false, then I change the attributes, and call updateItem(firstItem), it saves the uploaded state. But if there is more than one object in the array, it only saves one.



I tried to save it as JSON, and parse when I read it back, but the results are the same.










share|improve this question































    0















    I have an Ionic 3 app, where I store an array of objects in SQLite.



    My this.data array (explained in the lower section) looks like this:



    [
    {
    guid: "xy",
    images: [
    { guid: 0, uploaded: true },
    { guid: 1, uploaded: true }
    ],
    },
    {
    guid: "xz",
    images: [
    { guid: 0, uploaded: false },
    { guid: 1, uploaded: false }
    ],
    }
    ]


    So an object has a guid and an array of objects again. After an item is updated I want to save all the items to the storage as well as to the uploader class' variable. The uploader class has a this.data and this.key property.



    Here is a snippet from the problematic part:



    updateItem(value){
    // search for the index of the item in the array
    var index = this.data.findIndex((item) => {
    return item.guid == value.guid;
    });

    if(index >= 0){
    // update the item in the array with the changed one
    this.data[index] = value;

    // this works fine, prints with the updated item
    console.log(this.data);

    // it supposed to save the whole array with the changed items
    return this.storage.set(this.key, this.data).then(() => {

    // for debug I read the data again - same happens after app restart
    this.storage.get(this.key).then((data) => {
    // this logs the initial, unchanged items
    console.log(data);
    });
    });
    }

    return Promise.reject(new Error('error'));
    }


    First, it searches for the index of the item in the this.data array, if found, then overrides the item in the array.
    Then it tries to save it into the storage.
    For debugging purposes, I read the storage and console.log it.



    After I set the "xz" object's images to uploaded = true, I call the updateItem(secondItem).



    From the first console.log I see that "xy" and "xy" object's images are all uploaded: true. The storage.set called, and inside the storage.get, the initial state occurs. The "xy" object's images are uploaded: true, but the "xz" object's are false.
    After I restart my app, this state loads again.



    If there is only one object in this.data, the updateItem works fine, for example, I set the storage with uploaded: false, then I change the attributes, and call updateItem(firstItem), it saves the uploaded state. But if there is more than one object in the array, it only saves one.



    I tried to save it as JSON, and parse when I read it back, but the results are the same.










    share|improve this question



























      0












      0








      0








      I have an Ionic 3 app, where I store an array of objects in SQLite.



      My this.data array (explained in the lower section) looks like this:



      [
      {
      guid: "xy",
      images: [
      { guid: 0, uploaded: true },
      { guid: 1, uploaded: true }
      ],
      },
      {
      guid: "xz",
      images: [
      { guid: 0, uploaded: false },
      { guid: 1, uploaded: false }
      ],
      }
      ]


      So an object has a guid and an array of objects again. After an item is updated I want to save all the items to the storage as well as to the uploader class' variable. The uploader class has a this.data and this.key property.



      Here is a snippet from the problematic part:



      updateItem(value){
      // search for the index of the item in the array
      var index = this.data.findIndex((item) => {
      return item.guid == value.guid;
      });

      if(index >= 0){
      // update the item in the array with the changed one
      this.data[index] = value;

      // this works fine, prints with the updated item
      console.log(this.data);

      // it supposed to save the whole array with the changed items
      return this.storage.set(this.key, this.data).then(() => {

      // for debug I read the data again - same happens after app restart
      this.storage.get(this.key).then((data) => {
      // this logs the initial, unchanged items
      console.log(data);
      });
      });
      }

      return Promise.reject(new Error('error'));
      }


      First, it searches for the index of the item in the this.data array, if found, then overrides the item in the array.
      Then it tries to save it into the storage.
      For debugging purposes, I read the storage and console.log it.



      After I set the "xz" object's images to uploaded = true, I call the updateItem(secondItem).



      From the first console.log I see that "xy" and "xy" object's images are all uploaded: true. The storage.set called, and inside the storage.get, the initial state occurs. The "xy" object's images are uploaded: true, but the "xz" object's are false.
      After I restart my app, this state loads again.



      If there is only one object in this.data, the updateItem works fine, for example, I set the storage with uploaded: false, then I change the attributes, and call updateItem(firstItem), it saves the uploaded state. But if there is more than one object in the array, it only saves one.



      I tried to save it as JSON, and parse when I read it back, but the results are the same.










      share|improve this question
















      I have an Ionic 3 app, where I store an array of objects in SQLite.



      My this.data array (explained in the lower section) looks like this:



      [
      {
      guid: "xy",
      images: [
      { guid: 0, uploaded: true },
      { guid: 1, uploaded: true }
      ],
      },
      {
      guid: "xz",
      images: [
      { guid: 0, uploaded: false },
      { guid: 1, uploaded: false }
      ],
      }
      ]


      So an object has a guid and an array of objects again. After an item is updated I want to save all the items to the storage as well as to the uploader class' variable. The uploader class has a this.data and this.key property.



      Here is a snippet from the problematic part:



      updateItem(value){
      // search for the index of the item in the array
      var index = this.data.findIndex((item) => {
      return item.guid == value.guid;
      });

      if(index >= 0){
      // update the item in the array with the changed one
      this.data[index] = value;

      // this works fine, prints with the updated item
      console.log(this.data);

      // it supposed to save the whole array with the changed items
      return this.storage.set(this.key, this.data).then(() => {

      // for debug I read the data again - same happens after app restart
      this.storage.get(this.key).then((data) => {
      // this logs the initial, unchanged items
      console.log(data);
      });
      });
      }

      return Promise.reject(new Error('error'));
      }


      First, it searches for the index of the item in the this.data array, if found, then overrides the item in the array.
      Then it tries to save it into the storage.
      For debugging purposes, I read the storage and console.log it.



      After I set the "xz" object's images to uploaded = true, I call the updateItem(secondItem).



      From the first console.log I see that "xy" and "xy" object's images are all uploaded: true. The storage.set called, and inside the storage.get, the initial state occurs. The "xy" object's images are uploaded: true, but the "xz" object's are false.
      After I restart my app, this state loads again.



      If there is only one object in this.data, the updateItem works fine, for example, I set the storage with uploaded: false, then I change the attributes, and call updateItem(firstItem), it saves the uploaded state. But if there is more than one object in the array, it only saves one.



      I tried to save it as JSON, and parse when I read it back, but the results are the same.







      ionic3 ionic-storage






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 14:12









      Suraj Rao

      24k85973




      24k85973










      asked Nov 16 '18 at 12:14









      BarneyKBarneyK

      1289




      1289
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I ended up cloning the array, then saving the clone and then assign the clone to the original array. This solved my problem.



          updateItem(value){
          // search for the index of the item in the array
          var index = this.data.findIndex((item) => {
          return item.guid == value.guid;
          });

          var newData = this.data.slice();

          if(index >= 0){
          // update the item in the array with the changed one
          newData[index] = value;

          // it supposed to save the whole array with the changed items
          return this.storage.set(this.key, newData).then(() => {
          this.data = newData;

          // for debug I read the data again - same happens after app restart
          this.storage.get(this.key).then((data) => {
          // this logs the initial, unchanged items
          console.log(data);
          });
          });
          }

          return Promise.reject(new Error('error'));
          }





          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%2f53337713%2fionic-3-storage-set-and-get-acting-weird%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














            I ended up cloning the array, then saving the clone and then assign the clone to the original array. This solved my problem.



            updateItem(value){
            // search for the index of the item in the array
            var index = this.data.findIndex((item) => {
            return item.guid == value.guid;
            });

            var newData = this.data.slice();

            if(index >= 0){
            // update the item in the array with the changed one
            newData[index] = value;

            // it supposed to save the whole array with the changed items
            return this.storage.set(this.key, newData).then(() => {
            this.data = newData;

            // for debug I read the data again - same happens after app restart
            this.storage.get(this.key).then((data) => {
            // this logs the initial, unchanged items
            console.log(data);
            });
            });
            }

            return Promise.reject(new Error('error'));
            }





            share|improve this answer




























              0














              I ended up cloning the array, then saving the clone and then assign the clone to the original array. This solved my problem.



              updateItem(value){
              // search for the index of the item in the array
              var index = this.data.findIndex((item) => {
              return item.guid == value.guid;
              });

              var newData = this.data.slice();

              if(index >= 0){
              // update the item in the array with the changed one
              newData[index] = value;

              // it supposed to save the whole array with the changed items
              return this.storage.set(this.key, newData).then(() => {
              this.data = newData;

              // for debug I read the data again - same happens after app restart
              this.storage.get(this.key).then((data) => {
              // this logs the initial, unchanged items
              console.log(data);
              });
              });
              }

              return Promise.reject(new Error('error'));
              }





              share|improve this answer


























                0












                0








                0







                I ended up cloning the array, then saving the clone and then assign the clone to the original array. This solved my problem.



                updateItem(value){
                // search for the index of the item in the array
                var index = this.data.findIndex((item) => {
                return item.guid == value.guid;
                });

                var newData = this.data.slice();

                if(index >= 0){
                // update the item in the array with the changed one
                newData[index] = value;

                // it supposed to save the whole array with the changed items
                return this.storage.set(this.key, newData).then(() => {
                this.data = newData;

                // for debug I read the data again - same happens after app restart
                this.storage.get(this.key).then((data) => {
                // this logs the initial, unchanged items
                console.log(data);
                });
                });
                }

                return Promise.reject(new Error('error'));
                }





                share|improve this answer













                I ended up cloning the array, then saving the clone and then assign the clone to the original array. This solved my problem.



                updateItem(value){
                // search for the index of the item in the array
                var index = this.data.findIndex((item) => {
                return item.guid == value.guid;
                });

                var newData = this.data.slice();

                if(index >= 0){
                // update the item in the array with the changed one
                newData[index] = value;

                // it supposed to save the whole array with the changed items
                return this.storage.set(this.key, newData).then(() => {
                this.data = newData;

                // for debug I read the data again - same happens after app restart
                this.storage.get(this.key).then((data) => {
                // this logs the initial, unchanged items
                console.log(data);
                });
                });
                }

                return Promise.reject(new Error('error'));
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 31 '18 at 14:23









                BarneyKBarneyK

                1289




                1289
































                    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%2f53337713%2fionic-3-storage-set-and-get-acting-weird%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

                    List item for chat from Array inside array React Native

                    Thiostrepton

                    Caerphilly