Creating Pointer From Query Cloud Code

    //workplace is a pointer and workplace is string
    const query = new Parse.Query("Jobs");
    query.find({ useMasterKey: true }).then(function(results){
        for(i in results){
            var obj = results[i];
            var name = obj.get("jobName");
            var time = obj.get("workStart").toString();
            if (name == jobName) {
                if (time == modifiedDate) {
                    ticket.set("workplace", obj);
                    ticket.set("workplaceid", obj.id);
                }
            }
        }
    });

I am having difficulty setting a pointer after querying data. Am I missing something here? It won’t set the pointer but also has no errors.

Are you saving the ticket object? Maybe if you share the whole cloud function code it will be easier to help.

Parse.Cloud.define("createTicket", async (request) => {
    let currentUser = request.user;
    let jobName= request.params.job;
    let ticketDate = request.params.date;
    let Ticket = Parse.Object.extend("Ticket");
    let ticket = new Ticket();
    let modifiedDate = ticketDate + "Z";

    const query = new Parse.Query("Jobs");
    query.include("jobName");
    query.include("workStart");

    query.find({ useMasterKey: true }).then(function(results){
        for(i in results){
            var obj = results[i];
            var name = obj.get("jobName");
            var time = obj.get("workStart").toString();
            if (name == jobName) {
                if (time == modifiedDate) {
                    ticket.set("workplace", obj);
                    ticket.set("workplaceid", obj.id);
                }
            }
        }
    });

    ticket.set("user", currentUser);
    ticket.set("userObjectId", currentUser.id);
    ticket.set("jobDescription", jobName);
    ticket.set("startDate", ticketDate);
    ticket.save(null, {
        useMasterKey: true
    });
});

Here is the entire function. Basically it needs to link that Ticket class to a specific object in the Jobs Class which I assume I should do using a pointer but if I should use a relation instead let me know thanks.

You are not awaiting the find to complete and basically saving the ticket before the workplace setting code runs. Since you are using async function I’d remove the promise then and do something like this:

Parse.Cloud.define("createTicket", async (request) => {
    let currentUser = request.user;
    let jobName= request.params.job;
    let ticketDate = request.params.date;
    let Ticket = Parse.Object.extend("Ticket");
    let ticket = new Ticket();
    let modifiedDate = ticketDate + "Z";

    const query = new Parse.Query("Jobs");
    query.include("jobName");
    query.include("workStart");

    const results = await query.find({ useMasterKey: true });
        for(i in results){
            var obj = results[i];
            var name = obj.get("jobName");
            var time = obj.get("workStart").toString();
            if (name == jobName) {
                if (time == modifiedDate) {
                    ticket.set("workplace", obj);
                    ticket.set("workplaceid", obj.id);
                }
            }
        }

    ticket.set("user", currentUser);
    ticket.set("userObjectId", currentUser.id);
    ticket.set("jobDescription", jobName);
    ticket.set("startDate", ticketDate);
    ticket.save(null, {
        useMasterKey: true
    });
});

other tips (not related to your problem):

  • do not use var
  • use const instead of let whenever it’s possible
  • if you want to make sure that the cloud code function will complete only after the ticket is saved, you can await ticket.save(...)
  • Move Parse.Object.extend("Ticket") to outside the cloud function scope, so the class do not need to be created every time the function runs.

Alright first of all I want to thank you for all the help you have provided for my many questions. I’m a beginner to coding in general and I appreciate the help. Also with moving the Parse.Object.extend(“Ticket”) outside the function does that mean I should instead pass the Ticket name as a parameter or should I create the class object on my client sdk then pass it’s objectid to cloud code for it to be modified?

No. You just need literally to move the code outside the cloud function scope. Something like:

const Ticket = Parse.Object.extend("Ticket");
Parse.Cloud.define("createTicket", async (request) => {
    let currentUser = request.user;
    let jobName= request.params.job;
    let ticketDate = request.params.date;    
    let ticket = new Ticket();
    // ...
});