Joseph Ebey
Joseph Ebey

Reputation: 49

Parse.com Adding Cloud code to html buttons

I'm testing some functions locally using my database created on parse.com and the functions run exactly as intended. I am trying now to put these functions on cloud code to reduce the amount of request sent to parse as well as run the queries in the cloud code instead of locally. For whatever reason I cant get these functions to work when I convert them to cloud code. Also, how would I make a button in html that can run a function in cloud code?

Before cloud code, my html button looked like this:

<button type="button" onclick="authenticate()">Log-In</button>

How would I create that button with cloud code that looks like this:

Parse.Cloud.define("authenticate()", function(request, response) {
    var myname = document.getElementById("username").value;
    var mypass = document.getElementById("psswd").value;

    Parse.User.logIn(myname, mypass, {
        success: function(user) {
            // Do stuff after successful login.
             if(myname == "test1" || myname == "test2"){
                window.location.href="itSplash.html";
            }
            else{
                window.location.href="ticketRequest.html";
            }           
        }, 
        error: function(user, error) {
            // The login failed. Check error to see why.
            alert("Failed to login: " + error.message);
        }
    });
});

for clarification that if statement just directs login to itSplash if username matches test1 or test2, and ticketRequest if its anyone else. We have a seperate page for different users. Also, that function works locally if I create it as a normal function authenticate(). When I converted it to cloud code as seen above it wont work. I create a seperate function name runAuthenticate() with a Parse.Cloud.run call inside that and it wouldnt work there. All I got was an Error saying define cannot be used on that Object. Any Help?

Upvotes: 0

Views: 614

Answers (2)

sudheeshcm
sudheeshcm

Reputation: 3418

You can link the HTML button with a cloud code function by using Parse.Cloud.run.

Take a look at the Parse Cloud code documentations.

This is how you can call the cloud code from javascript.

Parse.Cloud.run('hello', {}, {
    success: function(result) {
        // result is 'Hello world!'
    },
    error: function(error) {
        //  Error while running cloud code
    }
});

As you have written, you can try calling a javascript method onclick on the HTML button and then call the Parse.Cloud.run method.

Upvotes: 2

Wain
Wain

Reputation: 119031

First, you don't have access to document or window in cloud code so you need to rethink where you're coming from.

Also, it isn't appropriate to have the user details sent to the cloud code, you should login on the web page using the SDK and then use the SDK to trigger the cloud code and it will send the user and auth details.

So, the whole premise of your authenticate function being in cloud code doesn't really work.

That doesn't mean you shouldn't use cloud code, it's just that you shouldn't use it for this purpose. You talk about making queries but you don't actually have any in the code you show - but that kind of thing is more likely to be movable to cloud code...

Upvotes: 0

Related Questions