Cannot create a pointer to an unsaved Object (Nested Objects)

Hello,

I have noticed that when saving a Parse object (parent) that has a child Parse object and the child object also contains a Parse object. Then when saving the parent object it throws an error (Cannot create a pointer to an unsaved Object).

For example, I have three objects Transport, Facility, and Location. The Transport object contains a Facility object and the Facility object contains a Location object.

When saving the transport object I will get the error, but if I comment out the location object in the facility object. Then save the transport object the transport and facility objects will save without error.

Is this a known problem when saving nested parse objects at least 3 levels deep? If so, is there a solution or workaround?

Here is a snippet of code that throws the error.

    var location: Location = new Location();
    location.address = "2279 Kings Hill";
    location.city = "Kings Vell Dr";
    location.state = "WA";
    location.postalCode = "98023";
    location.country = "USA";
    
    var location2: Location = new Location();
    location2.address = "311 Sage Alley";
    location2.city = "Skärholmen";
    location2.state = "Stockholm";
    location2.postalCode = "12786";
    location2.country = "Sweden";
    

    var facility1: Facility = new Facility();
    facility1.name = 'Facility 1';
    facility1.location = location;


    var facility2: Facility = new Facility()
    facility2.name = 'Facility 2',
    facility2.location = location2,


    var transport: Transport = new Transport();
    transport.sendingFacility = facility1;
    transport.receivingFacility = facility2;

    transport.save().then(transport => {
       console.log("Transport saved");
    }).catch(error => {
       console.log(error) // Cannot create a pointer to an unsaved Object
   });

If I do not include the location objects the Transport object and Facility objects are saved to the database.

    var facility1: Facility = new Facility();
    facility1.name = 'Facility 1';

    var facility2: Facility = new Facility()
    facility2.name = 'Facility 2',

    var transport: Transport = new Transport();
    transport.sendingFacility = facility1;
    transport.receivingFacility = facility2;

    transport.save().then(transport => {
       console.log("Transport saved");
    }).catch(error => {
       console.log(error);
   });

Thank you for your time.

You need to save your objects before you can add them as pointers to other objects. So call the save method to your facility 1 and facility 2 before creating transport. It will be much cleaner if you use async await in these cases.

You can do it, if you define beforeSave you can see;

Parse.Cloud.beforeSave("Transport ", async (req) => {
// console.log(“beforeSave”,req.object);
});
await transport.save()