flamelink / flamelink-js-sdk

🦊 Official Flamelink JavaScript SDK for both the Firebase Realtime database and Cloud Firestore
https://flamelink.github.io/flamelink-js-sdk
MIT License
43 stars 5 forks source link

Update method overwrites entry #118

Closed Nsingh13 closed 3 years ago

Nsingh13 commented 4 years ago

I'm using .update() in order to update a schema entry. It works but it overwrites the entire entry, removing whatever is in the other fields.

flameLinkApp.content.getByField({
        schemaKey: 'users',
        field: 'user.firstName',
        value: 'Test'
      }).then((data) => {
        console.log(data);
        let thisUserId = '';
        for(let id in data) {
          thisUserId = id;
          console.log(thisUserId);
        }
        flameLinkApp.content.update({
          schemaKey: 'users',
          entryId: thisUserId,
          data: {
            user: {
              firstName: 'Test complete',
            }
          }
        }).then(() => {
          console.log('User updated: '+thisUserId);
          //console.log('field: '+)
        }).catch((error) => {
          console.log('update error: '+error)
        });
      });      
Nsingh13 commented 4 years ago

Sorry, I don't know how to format the code properly. I'm fairly new to Github.

 flameLinkApp.content.getByField({
        schemaKey: 'users',
        field: 'user.firstName',
        value: 'Test'
      }).then((data) => {
        console.log(data);
        let thisUserId = '';
        for(let id in data) {
          thisUserId = id;
          console.log(thisUserId);
        }
        flameLinkApp.content.update({
          schemaKey: 'users',
          entryId: thisUserId,
          data: {
            user: {
              firstName: 'Test complete',
            }
          }
        }).then(() => {
          console.log('User updated: '+thisUserId);
          //console.log('field: '+)
        }).catch((error) => {
          console.log('update error: '+error)
        });
      });      
gitdubz commented 4 years ago

Thanks for rasing this @Nsingh13 we will look into it as soon as possible and let you know.

Nsingh13 commented 4 years ago

Ok, thanks @gitdubz . Please let me know. For now, I just appended the old data along with the update as a temporary fix.

jperasmus commented 4 years ago

Hi @Nsingh13 are you using Cloud Firestore or the Realtime Database?

Something in your code that looks a bit odd to me is the part where you iterate over each property in the returned data object and override your thisUserId variable with it. ie. thisUserId will have the value of whatever the last property was that the for loop iterated over and not necessarily the actual id property. I don't think this is right.

For the code formatting, you can check this link: https://help.github.com/en/github/writing-on-github/basic-writing-and-formatting-syntax

🙂

Nsingh13 commented 4 years ago

Hello @jperasmus! I am using Cloud Firestore.

I am looping through each property in order to obtain the entry id of the correct user so that I can use that to call the update function. I don't believe there is any other way of getting the entry id, is there? Maybe if you can provide me an example of how you would call the update function, that might be helpful!

Also, thanks for the formatting link :)

jperasmus commented 4 years ago

I think what you are looking for is the id property on the data response, so you should be able to access it with data.id. This code of yours loop through each property in the data array and override the thisUserId with each iteration:

for (let id in data) {
  thisUserId = id;
  console.log(thisUserId);
}

If your data object looks like this for instance: { a: 1, b: 2, c: 3 }, then you'll see a, b, c logged and thisUserId will be equal to c.

Can you console log the data object and show me how that looks?

jperasmus commented 4 years ago

Hi @Nsingh13 Do you still need help with this issue?

gitdubz commented 3 years ago

Hi @Nsingh13

Not sure if this is still something that you are struggling with. I can confirm that for nested fields the updates are not on an individual level when passing an object, and you should rather pass the path to the field

example

data: { "user.firstName": "Test Complete" } // updates a single field value

vs 

data: { user: { firstName: "Test Complete" } // updates the entire nested value

In order to update the values without overwriting the current values you can do it in the following 2 ways

Not recommended

      flameLinkApp.content
          .getByField({
            schemaKey: "users",
            field: "user.firstName",
            value: "Test",
          })
          .then((data) => {
            console.log(data);
            let thisUserId = "";
            for (let id in data) {
              thisUserId = id;
              console.log(thisUserId);
            }
            flameLinkApp.content
              .update({
                schemaKey: "users",
                entryId: thisUserId,
                data: {
                  user: {
                    ...data[thisUserId].user,
                    firstName: "Test complete",
                  },
                },
              })
              .then(() => {
                console.log("User updated: " + thisUserId);
                //console.log('field: '+)
              })
              .catch((error) => {
                console.log("update error: " + error);
              });
          });

Note this part is the only part changed form your original code.

  data: {
    user: {
        ...data[thisUserId].user,
        firstName: "Test complete",
     },
 },

Recommended

      flameLinkApp.content
          .getByField({
            schemaKey: "users",
            field: "user.firstName",
            value: "Test",
          })
          .then((data) => {
            console.log(data);
            let thisUserId = "";
            for (let id in data) {
              thisUserId = id;
              console.log(thisUserId);
            }
            flameLinkApp.content
              .update({
                schemaKey: "users",
                entryId: thisUserId,
                data: {
                  "user.firstName": "Test complete"
                },
              })
              .then(() => {
                console.log("User updated: " + thisUserId);
                //console.log('field: '+)
              })
              .catch((error) => {
                console.log("update error: " + error);
              });
          });

Note this part is the only part changed form your original code.

  data: {
    "user.firstName": "Test complete"
  },
gitdubz commented 3 years ago

Documentation required

gitdubz commented 3 years ago

Examples have been added here https://flamelink.github.io/flamelink-js-sdk/#/content?id=update