javascript - Meteor 中 Uncaught Error : Must set options. 密码

标签 javascript html authentication meteor

我正在尝试创建多个用户,因此我使用 Accounts.createUser 做了一个“创建用户”的示例。我还添加了一个包 accounts-baseaccounts-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.
        }
    });
}

最佳答案

问题出在这两行

 var email = document.getElementById('email');
 var password = document.getElementById('pwd');

getElementById 仅返回对元素的引用,而不返回值。

将它们更改为

 var email = document.getElementById('email').value;
 var password = document.getElementById('pwd').value;

关于javascript - Meteor 中 Uncaught Error : Must set options. 密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21178691/

相关文章:

javascript - 如何解决 Ajax 上的 Invalid JSON Primitive 错误?

javascript - 模糊图像 JavaScript

javascript - 在头部加载 Jquery 展开/折叠 Div

sql - 部署包含SQL Server数据库的应用程序。哪种认证方式?

java - 列出域中的 LDAP 服务器并进行身份验证

javascript - CSS3 蛇梯子/之字形布局

Javascript "for(i in array)"将 i 设置为最后一个索引

javascript - 无法让我的音板在智能手机上播放

macos - 为 safari/chrome mac 用户嵌入 HTML5 YouTube 视频开始在 div 中播放,显示无属性

java - 验证 Java Web Start 客户端应用程序