如何在 meteor.js 上保存其他用户详细信息,例如家庭住址
How to save additional user details on meteor.js, such as home address
我是 Meteor.js 的新手,并按照 meteor.com 上的教程创建了一个任务列表以登录和 post 任务。将 accounts-ui 和 accounts-password 包添加到项目中非常容易。但是,在注册时,我还希望用户提供家庭地址信息,甚至更多。我希望将此信息存储在链接到用户 ID 的 mongodb 中。你会怎么做?
您可以在用户的个人资料字段中添加信息。
活动中的类似内容(客户端代码)
Template.example.events({
'click #register':function(event,template){
var homeAddress = template.$('homeAddress').val();
Accounts.createUser({
email: email,
password: password,
profile: { homeAddress: homeAddress }
});
}
})
请记住,username
、email
和 profile
字段默认由帐户密码包发布。
您正在寻找的是使用 Accounts.createUser() 时配置文件的额外选项。查看文档以获取有关如何使用的更多详细信息:http://docs.meteor.com/#/full/accounts_createuser.
配置文件选项采用一个对象,该对象正在添加到与用户关联的数据库文档中。在尝试下面的例子后,运行下面的命令可以看到它存储在数据库中。
In the directory of your project:
$ meteor mongo
$ db.users.find()
下面的例子returns:
{ "_id" : "kJNaCtS2vW5qufwJs", "createdAt" : ISODate("2015-05-11T20:50:21.484Z"), "services" : { "password" : { "bcrypt" : "a$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" } }
作为警告,我没有使用 accounts-ui,只使用没有 ui 中的 built 的 accounts-password。但这里是如何创建一个带有额外字段的简单创建帐户模板(在这个例子中:名字和姓氏,以及组织)。
html代码:
<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代码:
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
});
}
在服务器端,您可以挂钩 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;
});
我是 Meteor.js 的新手,并按照 meteor.com 上的教程创建了一个任务列表以登录和 post 任务。将 accounts-ui 和 accounts-password 包添加到项目中非常容易。但是,在注册时,我还希望用户提供家庭地址信息,甚至更多。我希望将此信息存储在链接到用户 ID 的 mongodb 中。你会怎么做?
您可以在用户的个人资料字段中添加信息。
活动中的类似内容(客户端代码)
Template.example.events({
'click #register':function(event,template){
var homeAddress = template.$('homeAddress').val();
Accounts.createUser({
email: email,
password: password,
profile: { homeAddress: homeAddress }
});
}
})
请记住,username
、email
和 profile
字段默认由帐户密码包发布。
您正在寻找的是使用 Accounts.createUser() 时配置文件的额外选项。查看文档以获取有关如何使用的更多详细信息:http://docs.meteor.com/#/full/accounts_createuser.
配置文件选项采用一个对象,该对象正在添加到与用户关联的数据库文档中。在尝试下面的例子后,运行下面的命令可以看到它存储在数据库中。
In the directory of your project:
$ meteor mongo
$ db.users.find()
下面的例子returns:
{ "_id" : "kJNaCtS2vW5qufwJs", "createdAt" : ISODate("2015-05-11T20:50:21.484Z"), "services" : { "password" : { "bcrypt" : "a$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" } }
作为警告,我没有使用 accounts-ui,只使用没有 ui 中的 built 的 accounts-password。但这里是如何创建一个带有额外字段的简单创建帐户模板(在这个例子中:名字和姓氏,以及组织)。
html代码:
<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代码:
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
});
}
在服务器端,您可以挂钩 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;
});