简体   繁体   中英

parse.com Reference Error: user is not defined

I found this really cool site called http://parse.com . It is an online database (amongst many other things). Definitely check it out. Anyway, I believe I am one step away from figuring out how to use Parse.User, and render the objects email in an ejs template. I have it so that I can create and store users on parse.com - but I dont think that my template is recognizing the "user" variable I created. Here is my code and error:

application.js

$(document).ready(function() {
    $("#signupform").submit(function(e) {  
        e.preventDefault();

        Parse.initialize("O3LAmHCCGmMWBRuPEt4cXWP5ChQMjeruyOkMN7m1", "AIyuUVnGYbyQrWaYns7TL3XpmjPb1FDeplQ76DgG");

        var user = new Parse.User(); 
        user.setUsername($('#username').val());
        user.setEmail($('#email').val());
        user.setPassword($('#pw').val());

        user.signUp();

        var html = new EJS({url: 'templates/usershow.ejs'}).render(user);
        var content = document.getElementById('listuser');
        content.innerHTML = content.innerHTML + html;
        $("#signupform").remove();   
    });
});

templates/usershow.ejs

<li><%= user.getEmail() %></li>

index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>it IT</title>
  <link rel="stylesheet" type="text/css" href="application.css" />
  <script src="jquery.js"></script> 
  <script src="application.js"></script>
  <script type="text/javascript" src="ejs_production.js"></script>
  <script src="http://www.parsecdn.com/js/parse-1.2.7.min.js"></script>
  <!--[if lt IE 9]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
</head>
<body>
    <h1>it IT</h1>
    <div id="signup">
        <form id="signupform">
            Username: <input type="text" name="username" id="username"><br>
            Password: <input type="password" name="pw" id="pw"><br>
            Email: <input type="text" name="email" id="email"><br>
            <input type="submit" value="sign up">
        </form>
    </div>
    <div id="signin"></div>
    <div id="showuser">
        <h2>Username - Email</h2>
        <ul id="listuser"></ul>
    </div>
</body>
</html>

The error I get in firebug(after a successful post to the parse.com database) is:

ReferenceError: user is not defined

The line in question seems to be the var html = new EJS({url: 'templates/usershow.ejs'}).render(user); line. I'm not sure if the render function can recognize a Parse.User object? Maybe a problem using ejs with Parse.User?

UPDATE

In response to the comment below, I changed my code to this:

$(document).ready(function() {
    $("#signupform").submit(function(e) {  
        e.preventDefault();

        Parse.initialize("O3LAmHCCGmMWBRuPEt4cXWP5ChQMjeruyOkMN7m1", "AIyuUVnGYbyQrWaYns7TL3XpmjPb1FDeplQ76DgG");

        var user = new Parse.User(); 
        user.setUsername($('#username').val());
        user.setEmail($('#email').val());
        user.setPassword($('#pw').val());

        user.signUp(null, {
            success: function (user) {
                // Hooray! Let them use the app now.
                var html = new EJS({url: 'templates/usershow.ejs'}).render(user);
                var content = document.getElementById('listuser');
                content.innerHTML = content.innerHTML + html;
                $("#signupform").remove();   
            },
            error: function (user, error) {
                // Show the error message somewhere and let the user try again.
                //alert("Error: " + error.code + " " + error.message);
                console.log("Error: " + error.code + " " + error.message);                        
            }
        });
    });
});

While I think this corrected a problem with my code...I am still getting the same error, stated above.

Again, I'm coming back to this line var html = new EJS({url: 'templates/usershow.ejs'}).render(user); as the culprit...just because the user object is a Parse.User object and not just a regular object?

The call you are making here is asynchronous, you will need to write your code in the success block of the parse call, so it will be executed once the reply from parse is back to your app.

    var user = new Parse.User(); 
    user.setUsername($('#username').val());
    user.setEmail($('#email').val());
    user.setPassword($('#pw').val());

            newUser.signUp(null, {
                success: function (user) {
                    // Hooray! Let them use the app now.
                    var html = new EJS({url: 'templates/usershow.ejs'}).render(user);
                    var content = document.getElementById('listuser');
                    content.innerHTML = content.innerHTML + html;
                    $("#signupform").remove();   
                },
                error: function (user, error) {
                    // Show the error message somewhere and let the user try again.
                    //alert("Error: " + error.code + " " + error.message);
                    console.log("Error: " + error.code + " " + error.message);                        
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM