Meteor:在创建帐户时插入具有唯一 ID 的对象 [英] Meteor: insert object with unique id upon account creation
问题描述
创建用户帐户后,我想插入摘要".和用户配置文件中的newsInterest"对象,它们具有唯一的_id"值和text"字段
Upon user account creation, I would like to insert the 'summary. and 'newsInterest' objects within the user profile that have a unique '_id' value and a 'text' field
summary: // _id: must have unique when account is created
text:
newsInterest: // id:
text:
这样我就可以使用会话变量在按键时更新文本"值,具体取决于用户键入的输入区域.所以我需要 'summary' 和 'newsInterest' _id: 在用户创建帐户后立即可用.
This is so that I can use a session variable to update the 'text' values upon keyup depending on which input area the user is typing. So I need 'summary' and 'newsInterest' _id: immediately available after the user creates an account.
我的账户创建代码如下.
My account creation code is as follows.
Template.join.events({
'submit #join-form': function(e,t){
e.preventDefault();
var firstName= t.find('#join-firstName').value,
lastName= t.find('#join-lastName').value,
email = t.find('#join-email').value,
password = t.find('#join-password').value,
username = firstName + '.' + lastName,
profile = {
name: firstName + ' ' + lastName
};
Accounts.createUser({
email: email,
username: username,
password: password,
profile: profile
}, function(error) {
if (error) {
alert(error);
} else {
Router.go('home');
}
});
}
});
推荐答案
保持客户端 createUser 函数不变,但将附加对象附加到服务器上用户创建的配置文件中.这段代码应该这样做:
Keep your client side createUser function as it is but attach the additional objects to profile on user creation on the server. This code should do it:
Accounts.onCreateUser(function(options, user) {
profile = options.profile;
profile.summary = {
_id: Random.id(),
content: ''
};
profile.newsInterest = {
_id: Random.id(),
content: ''
};
user.profile = profile;
return user;
});
为了使用 Random.id,你需要安装随机包:meteor add random
In order to use Random.id you will need to install the random package with: meteor add random
这篇关于Meteor:在创建帐户时插入具有唯一 ID 的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!