Updating objects in the dashboard in frontend

Hello, could I get some help with updating the objects in the database from the frontend?

I want to make so that I can edit the object “User” username or email in frontend for example by clicking a button in a datatable. When the user signs up, the username and email gets saved in the datatable. I am also querying for the “User” Object using a cloud function. I am trying to simply update the User’s “username” to a new string.

Code in the cloud function:

Moralis.Cloud.define("getData", async (request) => {
  const query = new Moralis.Query("User");
  const results = await query.find({useMasterKey: true});
  return results;
});

Rest of the code:

async function getDataBrowser() {
  const User = Moralis.Object.extend("User");
  const user = new User();

  const data = await Moralis.Cloud.run("getData");
  console.log(data);

  for (let index = 0; index <= data.length; index++) {
    $(".dataBody").append(
      `<tr>
        <td>${data[index].attributes.username}<button type="button" class="usernameEdit${index} btn btn-sm btn-dark float-end">Edit</button></td>
        <td>${data[index].attributes.email}<button type="button" class="emailEdit${index} btn btn-sm btn-dark float-end">Edit</button></td>
      </tr>`);

    $(`.usernameEdit${index}`).click(() => {
      user.set("username", "usertest");
      user.save();
      console.log(user);
    });
  }
}

When I click on the button the username get’s changed to “usertest” in the console but I does not in the database, when I refresh the page the username is the same as in the begining.
Capture1

I get these errors when I click the edit button:
Capture
sometimes I also get almost the same error as above, but it says somethig like: “bad or username requiered”, instead of “pasword required”, not sure how I get the error.

Looked at the documentation uppdating objects and tried to use user.save().then((user) => { ... } like in the docs, but did not work.

Btw I also get this error when starting/refreshing page even though I correctly get the data of User’s username and email in the datatable, so the data[index].attributes.username should be set and not undefined. Maybe that could be causing a problem?
Capture

  1. it looks like in that cloud function you will get the data for all users on how it is written now, and not only on current user.
  2. on front end, It looks like that code creates a new user every time instead of updating current user info.

How you can do it:

x = await Moralis.User.current()
x.set("username", "new_usernname")
await x.save()
x = await Moralis.User.current()
x.get("username")
=>
'new_usernname'
1 Like

Ok thanks, I have tried to add the first part in the button click event: $(".usernameEdit${index}").click...

x = await Moralis.User.current()
x.set("username", "new_usernname")
await x.save()

but I get an error when I click on the button
Capture3
I also got another error before: “not enough auth.
Not sure if you meant that I should use the code like this.

I’m also not really sure how I should use this part:

x = await Moralis.User.current()
x.get("username")
=>
'new_usernname'

For some reason now after I click the button “Edit” the username changed after I refreshed but, when I wanted to change the username of “user 1” the username of “user 2” changed.

I want to change the usernames I click on, so that I can change anything in the database, using the frontend
Capture

it looks like you already have an user with that username, and that is why it is not working, works fine for me or if you try:

x = await Moralis.User.current()
x.set("username", "new_usernname2")
await x.save()
1 Like

Ok, thanks! I understand what you mean. It works, but only for the currently logged-in user. Is it possible to change the username of all the users? Because that’s what I wanted to try and do, i.e: if I’m “user 1”, I can change the username of users 2, 3, 4, etc.

Now it’s the usernames but it’s just for testing, I want to for example, be able to change all the names, health, etc. of the monsters that I own, change it in the data table. Is that possible?

It is possible to do it from a cloud function, using master key, is not possible in front-end because of the ACL that is set for user entries so that only current user can access its data

1 Like

I see that makes sense. Thanks for the help @cryptokid!