Problems with current implementation of Moralis Cloud Code

I’m having trouble reading token balance, incrementing it, and storing it back as a string.

Whenever I try out some function, the IDE (here VS Code) tells me that function is not available.

Currently, this is the code I use:

class PlayerReward extends Moralis.Object {
    constructor() {
        super("PlayerReward");
    }

    incrementRewards() {
        const rewardToAdd = Moralis.Cloud.units({
            method: "toWei", value: REWARD_PER_SIGNAL
        });
        const accumulatedReward = Moralis.Cloud.units({
            method: "toWei", value: this.get(KEY_ACCUMULATED_REWARD)
        });
        const newAccumulatedReward = Moralis.Cloud.units({
            method: "fromWei", value: accumulatedReward.plus(rewardToAdd).toString()
        });
        this.set(KEY_ACCUMULATED_REWARD, newAccumulatedReward);
    }
}
Moralis.Object.registerSubclass("PlayerReward", PlayerReward);

Obviously, it throws errors, such as this one:

2022-01-14T14:43:34.993Z - Error: Moralis units - Missing property `value`
    at Object.units (/moralis-server/lib/cloud-code/units.js:27:11)
    at PlayerReward.incrementRewards (eval at customUserPlugin (/moralis-server/cloud/main.js:140:21), <anonymous>:1:307)
    at Moralis.Cloud.define.fields (eval at customUserPlugin (/moralis-server/cloud/main.js:140:21), <anonymous>:1:1653)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-01-14T14:43:34.991Z - Failed running cloud function putWalletWasActiveInInterval for user TMxzXlbYaRxsQ1u3jT0uLkP4 with:
  Input: {"signal":true}
  Error: {"message":"Moralis units - Missing property `value`","code":141}

Most Cloud functions are not recognised in Typescript:

So it’s impossible to solve the problem without the IDE acting as a crutch.

Any assistance will be truly appreciated!

I don’t know what is the problem there, you could try to use bignumer directly with a syntax like: new Moralis.Cloud.BigNumber("100000000")

I wanted to say that you may be able to do what you want directly with big number without using units.

I’ve tried all of those things before. The issue with trying directly with BigNumber is that I may get floating point errors if I factor in the decimals to get a Wei value.

actually you will get floating point problems now if you use current implementation of Moralis.Cloud.units

Incredibly weirdly I’m getting an error for something which was working just yesterday when I run the query from the API Console:

2022-01-16T06:12:40.534Z - TypeError: Cannot read properties of undefined (reading 'getUsername')
    at Moralis.Cloud.define.fields (eval at customUserPlugin (/moralis-server/cloud/main.js:140:21), <anonymous>:1:1777)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-01-16T06:12:40.529Z - Failed running cloud function sendActivitySignal for user undefined with:
  Input: {"signal":"true"}
  Error: {"message":"Cannot read properties of undefined (reading 'getUsername')","code":141}

This refers to me defining

    const player: Moralis.User = request.user;

and later attempting to log the username with player.getUsername().
This was working absolutely fine yesterday without the 0.0.329 update.

maybe you were not authenticated this time

No, the user ID I’m testing for is definitely logged in/authenticated.

Problem has been solved!

1 Like