0

I am trying to create a Customer class object which has a one to one relation with the User class. But the object doesn't save without giving any error. Here is my code:

 Parse.Cloud.afterSave(Parse.User, function(request) {
 user = request.object;
 role_name = user.get("role_name");
 user_name = user.get("user_name");
 user_id = user.get("objectId");

  if (role_name == "customer"){
    user = request.object;
    console.log(" I am inside if else");

    var Customer = Parse.Object.extend("Customer");
    var cus = new Customer();
    cus.set("name2" , "albert")
    var relation = cus.relation("userId");
    relation.add(user);
    cus.save(); // Customer object should get saved here 

    cus.save(null, {
    success: function(cus) {
        // Execute any logic that should take place after the object is saved.
        console.log("I am working")
        alert('New object created with objectId: ' + cus.objectId);
    },
      error: function(error) {
        // handleParseError(error);
        console.log(error)
        // Execute any logic that should take place if the save fails.
        // error is a Parse.Error with an error code and message.
      }
    });
  }

});

Log when I run this:

  after_save triggered for _User for user qu808uKOgt:
  Input: {"object":{"createdAt":"2015-10-11T18:36:07.661Z","objectId":"qu808uKOgt","phone":"5678956475","role_name":"customer","updatedAt":"2015-10-11T18:36:07.661Z","username":"newuser16"}}
  Result: Success
  I am inside if else
  {"name2":"apple","userId":{"__op":"AddRelation","objects":      [{"__type":"Pointer","className":"_User","objectId":"qu808uKOgt"}]}}
Gautam
  • 1,754
  • 1
  • 14
  • 22
  • did you try to use `cus.save().then(function(saveObj){ console.log("I am working") },function(error){ console.log('error') });` – XenoN Oct 11 '15 at 18:50
  • @XenoN I tried but nothing happened – Gautam Oct 11 '15 at 18:59
  • Not even error log ? – XenoN Oct 11 '15 at 19:01
  • This is what I get - `after_save triggered for _User for user qu808uKOgt: Input: {"object":{"createdAt":"2015-10-11T18:36:07.661Z","objectId":"qu808uKOgt","phone":"5678956475","role_name":"customer","updatedAt":"2015-10-11T18:36:07.661Z","username":"newuser16"}} Result: Success I am inside if else {"name2":"apple","userId":{"__op":"AddRelation","objects":[{"__type":"Pointer","className":"_User","objectId":"qu808uKOgt"}]}}` – Gautam Oct 11 '15 at 19:03
  • Edited question with the log please check – Gautam Oct 11 '15 at 19:06
  • strange, btw did you really have to use relation between user and customer. I think in that case pointer could be good. `cus.set("userId" , user)` – XenoN Oct 11 '15 at 19:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91974/discussion-between-xenon-and-nik). – XenoN Oct 11 '15 at 19:10
  • Quick thought. What is your used Javascript SDK version? If it is the latest one, you should change it back to version 1.4.2 (in your global.json) file as Parse currently has a bug in one of their JS SDK versions. Here is a thread where I described how to solve it exactly - http://stackoverflow.com/q/32667395/4988014 – eschanet Oct 13 '15 at 16:17
  • @EricSchanet Thank You so much. I fixed this thing by creating a new cloud function. Will try and see if I face this issue but for me the `customer` object wasn't getting saved. Any idea why? – Gautam Oct 13 '15 at 16:53

1 Answers1

0

I fixed this bug by creating a new cloud function, which will be called immediately after the user sign's up.

Parse.Cloud.define('functionName', function(request, response){
  var currentUser = Parse.User.current();
  var role_name =  currentUser.get("role_name");

  if (role_name == "customer"){
  // Do something
  }
  else if (role_name == "service_provider"){
  // Do something else 
  }
)};
Gautam
  • 1,754
  • 1
  • 14
  • 22