0
votes

I tried to implement resend email verification link on parse for which i updated my user email id with the existing one which i think triggers the email verification again. But when i tried to do that using admin role i am not able to save the object. I search and various posts but no success.

Cloud Code:-

Parse.Cloud.define("resend_email_verification_link", function(request, response) {

Parse.Cloud.useMasterKey();
console.log("User name:" + request.params.user_find);
var user_name = request.params.user_find;

var mail_user = Parse.Object.extend("User");
var user_save = new mail_user();

var query_user = new Parse.Query(user_save);
query_user.equalTo('username', user_name);
query_user.find ({
    success: function(results) 
    {
        if (results.length > 0)
        {
            var email_user = "[email protected]"; //results[0].get("email");
            //var email_user = results[0].get("email");
            console.log("Reults find for user name "+ user_name + "email Id:" + email_user);

            results[0].set("email", email_user);

            results[0].save(null, {
                success:function () {
                console.log("Successfully saved");
            },
                error:function (error) {
                console.log("Could not save" + error.message);
                }
            });

            console.log("Dont mess with me ");
            response.success();
        } else {
            response.error("No User Exists");
        }
    },
    error: function(error) {
        response.error("Query Failed.Error = "+ error.message);
    }
}); 
});

Logs:-

I2015-12-16T18:48:15.750Z]v58 Ran cloud function resend_email_verification_link with: Input: {"user_find":"rahulsmarty"} Result: undefined

I2015-12-16T18:48:15.779Z]User name:rahulsmarty

I2015-12-16T18:48:15.833Z]Reults find for user name rahulsmartyemail Id:[email protected]

I2015-12-16T18:48:15.836Z]Dont mess with me

Please help me in resolving this issue.

1

1 Answers

0
votes

First of all, updating an email field with what already exists will NOT trigger the email verification resend on Parse. You need to update the filed with something different first, then update it again with the original email address to trigger the verification email. For this purpose I suggest using a unique identifier like the user objectId to build a dummy temporary email address, something like [email protected]. Save this first to the field and then update it again with your original email address.

Also bear in mind that when you save an object on Parse, you need to wait for that operation to finish before returning response.success() in your Cloud code. Parse save() function is an asynchronous method which means when it returns, its operation is NOT necessarily completed. An asynchronous method returns a Promise and you need to wait for this promise to be fulfilled before returning response.success() or response.error(). Make sure to read and understand the concept of Promise in Prase documentation: https://www.parse.com/docs/js/guide#promises