我发现这个名为http://parse.com的网站非常酷。它是一个在线数据库(在许多其他事情中)。绝对检查一下。无论如何,我相信我距离弄清楚如何使用Parse.User,并在ejs模板中渲染对象电子邮件。我有它,所以我可以在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.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);
作为罪魁祸首......仅仅因为用户对象是Parse.User对象而不仅仅是常规对象?
答案 0 :(得分:0)
你在这里进行的调用是异步的,你需要在解析调用的成功块中编写你的代码,所以一旦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);
}
});