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.