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.
I get these errors when I click the edit button:
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?