parse.com引用错误:用户未定义

parse.com Reference Error: user is not defined

本文关键字:用户 未定义 错误 com 引用 parse      更新时间:2023-09-26

我发现了一个很酷的网站http://parse.com。它是一个在线数据库(在许多其他东西中)。一定要去看看。无论如何,我相信我离弄清楚如何使用Parse还有一步之遥。用户,并在ejs模板中呈现对象email。我有它,所以我可以创建和存储用户parse.com -但我不认为我的模板是识别"用户"变量我创建。下面是我的代码和错误:

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();   
    });
});

模板/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>

我在firebug中得到的错误(在成功发布到parse.com数据库之后)是:

ReferenceError: user is not defined

这条线似乎是var html = new EJS({url: 'templates/usershow.ejs'}).render(user);线。我不确定渲染函数是否可以识别Parse。用户对象?也许是一个问题使用ejs与Parse.User?

作为对下面注释的回应,我将代码修改为:

$(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);                        
            }
        });
    });
});

虽然我认为这纠正了我的代码的问题…我仍然得到相同的错误,如上所述。

再一次,我回到这行var html = new EJS({url: 'templates/usershow.ejs'}).render(user);作为罪魁祸首…仅仅因为用户对象是一个Parse。用户对象而不是普通对象?

你在这里进行的调用是异步的,你需要在parse调用的成功块中编写代码,所以它将在parse返回到你的应用程序的回复后执行。

    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);                        
                }
            });