Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
maryamzafar opened this issue Feb 13, 2017 · 2 comments
Closed

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

maryamzafar opened this issue Feb 13, 2017 · 2 comments

Comments

@maryamzafar
Copy link

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":"[email protected]"}
      Result: "p66qm34jd80p0j6ne03fe1q7f" functionName=assignTokenToUser, [email protected], user=undefined
    going to send an email... with result: p66qm34jd80p0j6ne03fe1q7f
    fullLink: https://beatthegym.com/[email protected]&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":"[email protected]"}
   Result: "tqc8m9lo2tcsrqn69c3q0e1q7f" functionName=assignTokenToUser, [email protected], user=undefined
 inside user.save...
 ^[[32minfo^[[39m: Ran cloud function assignTokenToUser for user undefined with:
   Input: {"email":"[email protected]"}
   Result: undefined functionName=assignTokenToUser, [email protected], 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..

@hramos
Copy link
Contributor

hramos commented Feb 13, 2017

Thanks for your question!

We want to make sure to keep signal strong in the GitHub issue tracker – to make sure that it remains the best place to track issues that affect the development of Parse Server.

Questions like yours deserve a purpose-built Q&A forum. Would you like to post this question to Stack Overflow with the tag #parse.com? We'll be happy to answer there. Post a link to your Stack Overflow question here, so that we don't lose track of it.

You may also use Server Fault for questions about managing your own servers.

@hramos hramos closed this as completed Feb 13, 2017
@maryamzafar
Copy link
Author

Thank you So much for the guidance - I have posted the question at the following link. Still stuck in the issue.

http://stackoverflow.com/questions/42215304/cloud-code-error-in-saving-user

Waiting for your guidance!

Maryam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants