I am new to Meteor.js and followed the tutorial on meteor.com to create a task list to log into and post tasks. It was very easy to add the accounts-ui & accounts-password packages to the project. However when signing up, I also want the user to provide home address information and possibly more. I want this information to be stored in mongodb linked to the id of the user. How would you do this?
我是Meteor.js的新手,并按照meteor.com上的教程创建一个任务列表来登录和发布任务。将accounts-ui和accounts-password包添加到项目中非常容易。但是,在注册时,我还希望用户提供家庭住址信息以及更多信息。我希望这些信息存储在链接到用户id的mongodb中。你会怎么做?
3 个解决方案
#1
You can add the information on the profile field of the user.
您可以在用户的配置文件字段中添加信息。
Something like this on the event (client side code)
事件上有这样的事情(客户端代码)
Template.example.events({
'click #register':function(event,template){
var homeAddress = template.$('homeAddress').val();
Accounts.createUser({
email: email,
password: password,
profile: { homeAddress: homeAddress }
});
}
})
Remember that username
,email
and profile
fields are published by default by the accounts-password package.
请记住,帐户密码包默认发布用户名,电子邮件和个人资料字段。
#2
What you are looking for is the extra option of profile when using Accounts.createUser(). Check the documentation for more details on how to use: http://docs.meteor.com/#/full/accounts_createuser.
您正在寻找的是使用Accounts.createUser()时配置文件的额外选项。有关如何使用的更多详细信息,请查看文档:http://docs.meteor.com/#/full/accounts_createuser。
The profile option takes an object which is adding to the database document associated with the user. After trying the example below, run the following commands to see that it is stored in the database.
配置文件选项接受一个对象,该对象将添加到与用户关联的数据库文档中。尝试下面的示例后,运行以下命令以查看它是否存储在数据库中。
In the directory of your project:
$ meteor mongo
$ db.users.find()
the following example returns:
以下示例返回:
{ "_id" : "kJNaCtS2vW5qufwJs", "createdAt" : ISODate("2015-05-11T20:50:21.484Z"), "services" : { "password" : { "bcrypt" : "$2a$10$LrcZ5lEOlriqvkG5pJsbnOrfLN1ZSCNLmX6NP4ri9e5Qnk6mRHYhm" }, "resume" : { "loginTokens" : [ { "when" : ISODate("2015-05-11T20:50:21.493Z"), "hashedToken" : "CXKwUhEkIXgdz61cHl7ENnHfvdLwe4f0Z9BkF83BALM=" } ] } }, "emails" : [ { "address" : "sharkbyte@someemail.com", "verified" : false } ], "profile" : { "firstName" : "shark", "lastName" : "byte" } }
Just as a warning, I am not using accounts-ui, only accounts-password without the built in ui. But here is how to create a simple create account template with extra fields (in this example: first & last name, and organization).
正如警告一样,我没有使用account-ui,只使用没有内置ui的帐户密码。但这里是如何使用额外字段创建一个简单的创建帐户模板(在此示例中:名字和姓氏,以及组织)。
html code:
<head>
<title>test</title>
</head>
<body>
<!-- checks if someone is logged in -->
{{#if currentUser}}
{{> userPage}}
{{else}}
{{> loginPage}}
{{/if}}
</body>
<template name="userPage">
<button id="logout">logout</button>
<p>You are logged in!</p>
</template>
<template name="loginPage">
<form><!-- create account form, use a different one for normal logging in -->
<input type="text" id="firstName" placeholder="First Name">
<input type="text" id="lastName" placeholder="Last Name">
<input type="text" id="organization" placeholder="Organization (optional)">
<input type="text" id="email" placeholder="Email Address">
<input type="password" id="password" placeholder="Password">
<input type="password" id="confirmPassword" placeholder="Confirm Password">
<input type="submit" id="createAccount" value="Create Account">
</form>
</template>
javascript code:
if (Meteor.isClient) {
Template.loginPage.events({
'submit form': function(event, template) {
event.preventDefault();
console.log('creating account');
var passwordVar = template.find('#password').value;
var confirmVar = template.find('#confirmPassword').value;
if (passwordVar === confirmVar) {
Accounts.createUser({
email: template.find('#email').value,
password: passwordVar,
// you can add wherever fields you want to profile
// you should run some validation on values first though
profile: {
firstName: template.find('#firstName').value,
lastName: template.find('#lastName').value
}
});
}
}
});
// make sure to have a logout button
Template.userPage.events({
'click #logout': function(event, template) {
Meteor.logout();
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
#3
On the server side you can hook into Accounts.onCreateUser
hook and capture additional fields.
在服务器端,您可以挂钩到Accounts.onCreateUser挂钩并捕获其他字段。
if (Meteor.isServer) {
Meteor.startup(function() {
});
Accounts.onCreateUser(function(options, user) {
if (options.profile) {
user.profile = options.profile;
}
user.profile.address =
user.profile.city =
user.profile.state =
user.profile.zip =
user.profile.anything =
return user;
});
#1
You can add the information on the profile field of the user.
您可以在用户的配置文件字段中添加信息。
Something like this on the event (client side code)
事件上有这样的事情(客户端代码)
Template.example.events({
'click #register':function(event,template){
var homeAddress = template.$('homeAddress').val();
Accounts.createUser({
email: email,
password: password,
profile: { homeAddress: homeAddress }
});
}
})
Remember that username
,email
and profile
fields are published by default by the accounts-password package.
请记住,帐户密码包默认发布用户名,电子邮件和个人资料字段。
#2
What you are looking for is the extra option of profile when using Accounts.createUser(). Check the documentation for more details on how to use: http://docs.meteor.com/#/full/accounts_createuser.
您正在寻找的是使用Accounts.createUser()时配置文件的额外选项。有关如何使用的更多详细信息,请查看文档:http://docs.meteor.com/#/full/accounts_createuser。
The profile option takes an object which is adding to the database document associated with the user. After trying the example below, run the following commands to see that it is stored in the database.
配置文件选项接受一个对象,该对象将添加到与用户关联的数据库文档中。尝试下面的示例后,运行以下命令以查看它是否存储在数据库中。
In the directory of your project:
$ meteor mongo
$ db.users.find()
the following example returns:
以下示例返回:
{ "_id" : "kJNaCtS2vW5qufwJs", "createdAt" : ISODate("2015-05-11T20:50:21.484Z"), "services" : { "password" : { "bcrypt" : "$2a$10$LrcZ5lEOlriqvkG5pJsbnOrfLN1ZSCNLmX6NP4ri9e5Qnk6mRHYhm" }, "resume" : { "loginTokens" : [ { "when" : ISODate("2015-05-11T20:50:21.493Z"), "hashedToken" : "CXKwUhEkIXgdz61cHl7ENnHfvdLwe4f0Z9BkF83BALM=" } ] } }, "emails" : [ { "address" : "sharkbyte@someemail.com", "verified" : false } ], "profile" : { "firstName" : "shark", "lastName" : "byte" } }
Just as a warning, I am not using accounts-ui, only accounts-password without the built in ui. But here is how to create a simple create account template with extra fields (in this example: first & last name, and organization).
正如警告一样,我没有使用account-ui,只使用没有内置ui的帐户密码。但这里是如何使用额外字段创建一个简单的创建帐户模板(在此示例中:名字和姓氏,以及组织)。
html code:
<head>
<title>test</title>
</head>
<body>
<!-- checks if someone is logged in -->
{{#if currentUser}}
{{> userPage}}
{{else}}
{{> loginPage}}
{{/if}}
</body>
<template name="userPage">
<button id="logout">logout</button>
<p>You are logged in!</p>
</template>
<template name="loginPage">
<form><!-- create account form, use a different one for normal logging in -->
<input type="text" id="firstName" placeholder="First Name">
<input type="text" id="lastName" placeholder="Last Name">
<input type="text" id="organization" placeholder="Organization (optional)">
<input type="text" id="email" placeholder="Email Address">
<input type="password" id="password" placeholder="Password">
<input type="password" id="confirmPassword" placeholder="Confirm Password">
<input type="submit" id="createAccount" value="Create Account">
</form>
</template>
javascript code:
if (Meteor.isClient) {
Template.loginPage.events({
'submit form': function(event, template) {
event.preventDefault();
console.log('creating account');
var passwordVar = template.find('#password').value;
var confirmVar = template.find('#confirmPassword').value;
if (passwordVar === confirmVar) {
Accounts.createUser({
email: template.find('#email').value,
password: passwordVar,
// you can add wherever fields you want to profile
// you should run some validation on values first though
profile: {
firstName: template.find('#firstName').value,
lastName: template.find('#lastName').value
}
});
}
}
});
// make sure to have a logout button
Template.userPage.events({
'click #logout': function(event, template) {
Meteor.logout();
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
#3
On the server side you can hook into Accounts.onCreateUser
hook and capture additional fields.
在服务器端,您可以挂钩到Accounts.onCreateUser挂钩并捕获其他字段。
if (Meteor.isServer) {
Meteor.startup(function() {
});
Accounts.onCreateUser(function(options, user) {
if (options.profile) {
user.profile = options.profile;
}
user.profile.address =
user.profile.city =
user.profile.state =
user.profile.zip =
user.profile.anything =
return user;
});