简体   繁体   中英

Complex function using Parse Server Cloud Code (looping and creating records)

After a night of trial and error I have decided on a much simpler way to explain my issue. Again, I have no JS experience, so I don't really know what I am doing.

I have 5 classes:

  • game - holds information about my games
  • classification - holds information about the user classes available in games
  • game_classifications - creates a one game to many classifications relationship (makes a game have mulitple classes)
  • mission - holds my mission information
  • mission_class - creates a one to many relationship between a mission and the classes available for that mission

Using Cloud Code, I want to provide two inputs through my Rest API being missionObjectId and gameObjectId.

The actual steps I need the code to perform are:

  • Get the two inputs provided {"missionObjectId":"VALUE","gameObjectId":"VALUE"}
  • Search the game_classifications class for all records where game = gameObjectID
  • For each returned record, create a new record in mission_class with the following information:
    • mission_id = missionObjectId
    • classification = result.classification

Here is an image of the tables: 在此处输入图像描述

And here is how I have tried to achieve this:

Parse.Cloud.define("activateMission", async (request) => {
    Parse.Cloud.useMasterKey();
    const query = new Parse.query('game_classifications');
    query.equalTo("gameObjectId", request.params.gameObjectId);
    
    for (let i = 0; i < query.length; i ++) {
        const mission_classification = Parse.Object.extend("mission_class");
        const missionClass = new mission_classification();
        
        missionClass.set("mission_id", request.params.missionObjectId);
        missionClass.set("classification_id", query[i].classificationObjectId);
        return missionClass.save();
    }
});

Does anyone have any advice or input that might help me achieve this goal?

The current error I am getting is:

Parse.query is not a constructor

Thank you all in advance!

Some problems on your current code:

  • Parse.Cloud.useMasterKey() does not exist for quite a long time. Use useMasterKey option instead.
  • It's Parse.Query and not Parse.query .
  • You need to run query.findAll() command and iterate over it (and not over query).
  • For performance, move Parse.Object.extend calls to the beginning of the file.
  • To access the field of an object, use obj.get('fieldName') and not obj.fieldName .
  • If you return the save operation, it will save the first object, return, and not save the others.

So, the code needs to be something like this:

const mission_classification = Parse.Object.extend("mission_class");

Parse.Cloud.define("activateMission", async (request) => {
    const query = new Parse.Query('game_classifications');
    query.equalTo("gameObjectId", request.params.gameObjectId);
    
    const queryResults = await query.findAll({useMasterKey: true});

    for (let i = 0; i < queryResults.length; i++) {        
        const missionClass = new mission_classification();
        
        missionClass.set("mission_id", request.params.missionObjectId);
        missionClass.set("classification_id", queryResults[i].get('classificationObjectId'));
        await missionClass.save(null, { useMasterKey: true });
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM