Changing field value in the DB with a cloud function called from frontend

Hello, I’m trying just for testing to write on the database on a collection where the CLP permission is read only for public using a simple testing cloud function that I call from frontend.

This is the frontend code

const params =  { name: "London" };
const nameChange = await Moralis.Cloud.run("testNameChange", params);
console.log(nameChange);

This is the cloud code

Moralis.Cloud.define("testNameChange", async (request) => {
  const userAddress = request.user.get('ethAddress');
  const query = new Moralis.Query("Cities");
  query.equalTo("User", userAddress, 'i');
  const cityItem = await query.first();
 
  if (cityItem) {
    cityItem.set("cityName",  request.params.name);
    cityItem.save({useMasterKey:true});
    
  }
  
},{
  requireUser: true
});

Console log seems working on the variables (it retrieves the correct row) but nothing is written in the database.

Where could be the issue?

Thank you

You have to use .save(null, {useMasterKey: true})

1 Like

Yes I’ve tried and it worked, Thank you a lot

1 Like