我发现这个很酷的网站叫做http://parse.com。它是一个在线数据库(除其他外)。一定要去看看。无论如何,我相信我离找出如何使用parse.user和在ejs模板中呈现对象email还有一步之遥。我拥有它,这样我就可以在parse.com上创建和存储用户——但我认为我的模板没有识别我创建的“user”变量。这是我的代码和错误:
应用程序.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>

索引文件
<!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.user对象?也许在parse.user中使用ejs有问题?
更新
针对下面的评论,我将代码改为:
$(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);作为罪魁祸首…仅仅因为user对象是parse.user对象,而不仅仅是普通对象?

最佳答案

您在这里进行的调用是异步的,您需要在parse调用的success块中编写代码,因此一旦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);
                }
            });

10-06 12:33
查看更多