Skip to content

Parse Cloud Code - Saving User Gives Code 206 Error #3503

Closed
@maryamzafar

Description

@maryamzafar

Hello...

I have a function in my cloud code, which works, but I'm not sure how to fix a problem related to it.

Original Problem:

Parse.Cloud.define("assignTokenToUser", function(request, response) {
     console.log("Inside assignTokenToUser");
     var token = Math.random().toString(30).substring(7);
     query = new Parse.Query("User"),
     email = request.params.email;
 
     query.equalTo("username", email);
 
 
    query.find({ useMasterKey: true }).then(function(results) {
       query.first({
         success: function(user) {
           // Successfully retrieved the object.
           user.set("emailToken", token);
           user.save();
           console.log("success...");
           response.success(token);
         },
         error: function(error) {
           console.log("error 1...");
           response.error(error);
         }
       });
    }, function(error) {
      console.log("error 2...");
      response.error(error);
    });
 });

This seemed to be a common problem after scanning the internet, and my analysis is that the useMasterKey needs to be passed each time we use the query object. Correspondingly, my log file shows that when trying to save the user, it gives a Code 206 error.

Log file output:

    Inside assignTokenToUser
    success...
    ^[[32minfo^[[39m: Ran cloud function assignTokenToUser for user undefined with:
      Input: {"email":"maryam.zafar@emumba.com"}
      Result: "p66qm34jd80p0j6ne03fe1q7f" functionName=assignTokenToUser, email=maryam.zafar@emumba.com, user=undefined
    going to send an email... with result: p66qm34jd80p0j6ne03fe1q7f
    fullLink: https://beatthegym.com/emailVerified?username=maryam.zafar@emumba.com&token=p66qm34jd80p0j6ne03fe1q7f
    ^[[31merror^[[39m: Error generating response. ParseError { code: 206, message: 'Cannot modify user 4m0VZFsKVt.' } code=206, message=Cannot modify user 4m0VZFsKVt.
    [object Object]

So I went on to change my code to the following:

Code:

Parse.Cloud.define("assignTokenToUser", function(request, response) {
      console.log("Inside assignTokenToUser");
     var token = Math.random().toString(30).substring(7);
     query = new Parse.Query("User"),
     email = request.params.email;
 
     query.equalTo("username", email);
 
 
     query.find({ useMasterKey: true }).then(function(results) {
       console.log("inside query.find...");
       query.first(null, { useMasterKey: true }).then(function(user) {
           console.log("inside query.first...");
           // Successfully retrieved the object.
           user.set("emailToken", token);
           user.save(null, { useMasterKey: true }).then(function() {
              console.log("inside user.save...");
              response.success();
           }, function(error) {
                 response.error(error);
           });
           response.success(token);
         },
         function(error) {
           console.log("error 1...");
           response.error(error);
         });
    }, function(error) {
      console.log("error 2...");
      response.error(error);
    });
  });

Log file:

Inside assignTokenToUser
 inside query.find...
 inside query.first...
 ^[[32minfo^[[39m: Ran cloud function assignTokenToUser for user undefined with:
   Input: {"email":"maryam.zafar@emumba.com"}
   Result: "tqc8m9lo2tcsrqn69c3q0e1q7f" functionName=assignTokenToUser, email=maryam.zafar@emumba.com, user=undefined
 inside user.save...
 ^[[32minfo^[[39m: Ran cloud function assignTokenToUser for user undefined with:
   Input: {"email":"maryam.zafar@emumba.com"}
   Result: undefined functionName=assignTokenToUser, email=maryam.zafar@emumba.com, user=undefined
 [object Object]

Now, the log file gives me a user as "undefined", and the call to the function gives me a pending status in the Chrome Network tab in the Inspector tool, until it turns into 502, and then the request is auto generated by the browser again. All other requests get a correct 200 response.

However, the data seems to be saved.. the record against this email address saves the token generated correctly. But the request from the browser fails and the user is "undefined" while in the original log file, I see the correct user Id... everytime it fails, the function automatically runs again (because the browser is generating another request everytime it gets a 502) and since it is actually supposed to send an email, it's running again and again keeps on generating infinate emails...

Thank you in advance..

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions