1

I know this question has been asked on here once before but I need to know if parse has done anything about it. My default user table has a pointer field to a UserProfile class. On signup I have this cloud code below.

Parse.Cloud.beforeSave(Parse.User,async (request)=>{
      const user = request.object;
     //Making a new Teacherprofile Class
     const Objectextension = Parse.Object.extend("TeacherProfile");
     const teacherProfile = new Objectextension();
     teacherProfile.set("name", "harry");
     //Putting teacher profile pointer
     user.set("tProfile",teacherProfile);
    });

This just dosent not work and results in a timeout.Is there anyway to create the userprofile on before save and associate it to the User Table ? Thanks

UPDATE

This is the working code.

Parse.Cloud.beforeSave(Parse.User,async (request)=>{
      const user = request.object;

      //if user id does not exists
      //it is a new user
      if (!user.id) {
        //Making a new User Profile Object
        const profileObject = Parse.Object.extend("TeacherProfile");
        const teacherProfile = new profileObject();
        teacherProfile.set("name", "harry");
        await teacherProfile.save(null,{ useMasterKey: true });
        //Putting teacher profile pointer in user
        user.set("tProfile",teacherProfile);
      }else{
        console.log('old user');
      }
    });
Tanzim Chowdhury
  • 3,020
  • 2
  • 9
  • 21
  • 1
    Is there any beforeSave or afterSave trigger for 'TeacherProfile' ? – Suat Karabacak Feb 22 '20 at 07:16
  • 1
    No other handler, but it seems as if there was a scheme miss Match for the pointer in user and for that reason I think it went into a loop, now its completely fine :D Thanks for you comment – Tanzim Chowdhury Feb 22 '20 at 07:33

2 Answers2

1

After a bit more of experimentation I have come to the conclusion that before save is not at all advisable for User Profile Creation. When signing up lets say the username or email already exists , then the signup does not happen but the profile is saved regardlesss. So I would advice against it

Tanzim Chowdhury
  • 3,020
  • 2
  • 9
  • 21
0

Or you can use this code.

Parse.Cloud.afterSave(Parse.User, (request) => {

  if(!request.original){
    //Object saved for first time. This codes will work just first time. And will not work after object saved again.
    const user = request.object;

    const profileObject = Parse.Object.extend("TeacherProfile");
    const teacherProfile = new profileObject();
    teacherProfile.set("name", "harry");
    await teacherProfile.save(null,{ useMasterKey: true });
    //Putting teacher profile pointer in user
    user.set("tProfile",teacherProfile);
    user.save(null,{useMasterKey:true});

  }

});
uzaysan
  • 583
  • 1
  • 4
  • 18