我正在尝试创建多个用户,因此我使用Accounts.createUser
做了“创建用户”的示例。我还添加了一个包accounts-base
和accounts-password
。但是我收到了错误Must set options.password in Meteor
。我不知道发生了什么。查看下面的代码。
当时只有点击注册按钮我才收到错误
HTML:
<head>
<title>loginapp</title>
</head>
<body>
{{> main}}
</body>
<template name="main">
<form id="login-form" action="action">
<div>
<h2>Login<h2>
<input type="text" id="email" placeholder="Email" /><br>
<input type="password" id="pwd" placeholder="Password" /><br>
<input type="submit" value="Login" id="login" />
</div>
</form>
<form id="register-form" action="action">
<div>
<h2> Create Account<h2>
<input type="text" id="username" placeholder="Enter UserName" /><br>
<input type="text" id="name" placeholder=" Enter Name" /><br>
<input type="text" id="email" placeholder=" Enter Email" /><br>
<input type="password" id="pwd" placeholder=" Enter Password" /><br>
<input type="submit" value="Register" id="register" />
</div>
</form>
</template>
使用Javascript:
if (Meteor.isClient) {
Template.main.events({
'submit #login-form': function (e, t) {
// template data, if any, is available in 'this'
console.log("You pressed the button LOGIN ");
e.preventDefault();
// retrieve the input field values
var email = t.find('#email').value
, password = t.find('#pwd').value;
console.log(email);
Meteor.loginWithPassword(email, password, function (err) {
if (err) {
console.log(err);
Session.set("loginError", true);
} else {
console.log(" Login Success ");
}
});
}
});
Template.main.events
({
'submit #register-form': function (e, t) {
console.log("You pressed the button Register ");
e.preventDefault();
var username = t.find('#username').value
, name = t.find('#name').value
, email = t.find('#email').value
, password = t.find('#pwd').value;
Accounts.createUser({email: email, password: password, username: username, }, function (err) {
if (err) {
console.log(err);
}
else {
console.log("Register Successfully");
}
});
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
if (Meteor.users.find().count() === 0) {
Accounts.createUser
({
username: 'username',
email: 'abc.n@gmail.com',
password: '123456',
profile: {
first_name: 'fname',
last_name: 'lname',
company: 'CubeTech',
}
}) //Added close parenthesis.
}
});
}
答案 0 :(得分:2)
问题在于这两行
var email = document.getElementById('email');
var password = document.getElementById('pwd');
getElementById
仅返回对元素的引用,而不返回值。
将其更改为
var email = document.getElementById('email').value;
var password = document.getElementById('pwd').value;
答案 1 :(得分:-2)
您必须为项目添加基于帐户的包。