Incrementing _User data?

Hi there,

I’m trying to build a reputation system that will increment or decrement another users reputation based on the rating they give them. However, with the code below increment doesn’t work. I console logged the data to make sure it was coming through correctly but it turns out that it won’t even query the user data at all, so of course the find wouldn’t work. I added the master key to the find function but it still did nothing. Any suggestions?

const BuyerRateRepIncrease = async (buyer, rating) => {

  const query = new Moralis.Query('_User');

  // query.equalTo("ethAddress", buyer);

  const results = await query.find();

  console.log(buyer, query)

// if (rating == 1) {

//   const repPointIncrement = results.decrement('reputation_points', 50)

//   const repIncrement = results.decrement('reputation', 50)

//   await repPointIncrement.save()

//   await repIncrement.save()

(I’ve just commented out the code for now so that’s why it’s presented like that)

If you want to query data about another user you will have to use a cloud function and also to use master key parameter

Would the incremeting need to be done in the backend cloud function?

so if I had a cloud function like this:

Moralis.Cloud.define(“earningsUpdate”, async (seller, earningsOnProd) => {

const query = new Moralis.Query("reputation_earnings");

query.equalTo("user", seller);

const results = await query.first({ useMasterKey: true });

const amount = parseInt(earningsOnProd)

results.increment("earnings", amount, { useMasterKey: true })

await results.save({ useMasterKey: true })

});

How would i then call it in another js file?

same way as you would call any other cloud function, one way could be to use Moralis.Cloud.run("earningsUpdate", {})

thanks for getting back.

I tried it with:

const earningUpdate = async (seller, earningsOnProd) => {

const EarnedUpdate = await Moralis.Cloud.run('earningsUpdate', seller, earningsOnProd);

if (EarnedUpdate){

  alert('Earning update success')

}

};

but got invalid function in the console

Try first with a simpler function, like one that doesn’t have extra parameters and only the implicit request parameter. The parameter for a cloud function is named request. And in that request you will find the other parameters.

Okay, thanks for the help!