Update a user record with a pointer using Parse Cloud Code

177 Views Asked by At

I have viewed all the articles on here, and haven't done any javascript coding in the past. Hoping someone can help me.

I have a class called rank and of course the parse _User class. I have added a pointer to the _User class to the rank class (the column name is called user_rank, which allows me to give a user a rank - seems simple enough.

What I am trying to achieve, is to use Cloud Code to change a user's rank as the administrator of the app (so it's something I do in my admin app, not the user does in their app).

This is what I have, but all I get is an error 101 'Object not found'. I have no doubt I am doing this all wrong, but I have tried to piece together responses from other posts with no success.

Any help is greatly appreciated.

Updated code with Davi's change below - now throwing error schema mismatch for _User.user_rank; expected Pointer but got String

    Parse.Cloud.define("setUserRank", async (request, response) => {
    let { userObjectId, rankObjectId } = request.params;
    
    const userQuery = new Parse.Query(Parse.User);
    const rankQuery = new Parse.Query('rank');
    
    // Get the user object to change the rank of
    try{
        let user = await userQuery.get(userObjectId, { useMasterKey: true});
        let rank = await rankQuery.get(rankObjectId, { useMasterKey: true});
        console.log(user);
        console.log("Running");
        const rankRelation = user.relation('user_rank');
        rankRelation.add(user_rank);
        user.save(null, { useMasterKey: true});
        return ("User Rank Changed"));
    } catch (err) {
        throw new Error(err.message)
    }
});
1

There are 1 best solutions below

2
Davi Macêdo On

I think the problem happens because of this line:

const rankQuery = new Parse.Query(Parse.rank);

In the case of you custom classes, you need to pass the class name as a string:

const rankQuery = new Parse.Query('rank');