2014-10-22 82 views
0

这可能比我发出声音更简单。流星:在新创建的用户上插入文档

我允许我的用户在登录时创建自己的个人档案。这是存储在MyProfile = new Meteor.Collection('myprofile');中的文档。该原则与LinkedIn ...完全相同,您只需填写个人资料表单和所做的任何编辑即可更新该文档。

在文档中会有诸如'summary''newsInterest'之类的字段,还有'owner'是用户Id。

1)如何将文档插入MyProfile集合,其'owner'字段是StartUp上新创建用户的userId?

这是这样的,这个文件的数据,这些字段的值将被传递到myprofile页面。最初返回的值将是空白的,但随着用户键入,在键入时,myprofile文档将被更新。

在客户端上创建用户如下。现在很好。

2)另外,如果用户在服务器上创建了用户,请提供任何链接。我调用了一个方法将以下内容作为对象插入到Meteor.users.insert(object);中,但这不起作用。

Template.join.events({ 
'submit #join-form': function(e,t){ 
e.preventDefault(); 
Accounts.createUser({ 
    username: t.find('#join-username').value, 
    password: t.find('#join-password').value, 
    email: t.find('#join-email').value, 
    profile:{ 
    fullname: t.find('#join-fullname').value, 
summary: [], 
newsInterest: [], 
otherstuff: [] 
    } 
}); 
Router.go('myprofile'); 
} 
}); 
+0

我建议将Router.go('myprofile')添加到createUSer回调中,这样如果出于某种原因createUser错误,它将不会尝试更改页面,并且可以向用户显示错误。看到我的代码如下。 – 2014-10-22 10:46:21

回答

2

1)为了解决问题一你有两个选择。

而不是像在常规MySQL数据库中那样为配置文件分别收集配置文件。在已附加到用户集合中的对象的配置文件对象中添加用户配置文件数据。然后,您可以通过在你想要的值的Accounts.createUser function

Template.join.events({ 
    "submit #join-form": function (event) { 
    event.preventDefault(); 
    var firstName = $('input#firstName').val(), 
    lastName = $('input#lastName').val(), 
    username = firstName + '.' + lastName, 
    email = $('input#email').val(), 
    password = $('input#password').val(), 
    profile = { 
     name: firstName + ' ' + lastName 
    }; 
    Accounts.createUser({ 
     email: email, 
     username: username, 
     password: password, 
     profile: profile 
    }, function(error) { 
     if (error) { 
     alert(error); 
     } else { 
     Router.go('myprofile'); 
     } 
    }); 
    } 
}); 

的选项参数这是使用jQuery来获取值的例子,但你的t.find应该一样精细。

如果你确实想使用一个单独的集合,然后我建议使用onCreateUser function(服务器端)的内部下面的代码来代替:当你想更新或者添加额外的数据到配置文件场

Accounts.onCreateUser(function(options, user) { 
    user._id = Meteor.users._makeNewID(); 

    profile = options.profile; 
    profile.userId = user._id; 

    MyProfile.insert(profile); 

    return user; 
}); 

对于用户,您可以使用以下方法:

var newProfile = { 
    summary: 'This summary', 
    newsInterest: 'This newsInterest', 
    otherstuff: 'Stuff' 
}; 

Meteor.users.update(Meteor.userId, {$set: {profile: newProfile}}); 

或者,如果你去的单独收集选项如下:

var newProfile = MyProfile.findOne(Meteor.userId); 
newProfile.summary = 'This summary'; 
newProfile.newsInterest = 'This newsInterest'; 
newProfile.otherstuff = 'Stuff'; 

MyProfile.update(Meteor.userId, newProfile); 

没有测试过,所以让我知道如果我有任何语法/拼写错误,我会更新。

+0

嗨,我想为我的目的,这将是理想的简单存储在用户的配置文件信息,而不是一个集合。感谢您指出这个选项。 onCreateser代码是否存储在服务器上?感谢您和richsilv的贡献。 顺便说一句,在你的代码中的事件,应该姓氏是一个变量以及?因为,即时通讯使用t.find方法这部分代码在我的项目中不需要的 – meteorBuzz 2014-10-22 11:04:53

+0

叶通常把它放在/server/accounts.js文件中。 您可以在第四个参数中传递您想要的任何变量。在我的情况下,我连接firstName和lastName变量并将它们存储在profile.name中,但您可以在其中添加任何您喜欢的内容。 – 2014-10-22 11:09:57

+0

我刚刚重新阅读了onCreateUser的文档,它看起来会在没有附加代码的情况下将配置文件对象添加到用户。我在我的account.js文件中使用它的唯一原因是因为我使用oAuth登录名,并且必须将多个登录服务绑定到一个帐户。如果您使用没有服务器端accounts.js文件的事件代码,则应该没问题。 – 2014-10-22 11:12:47

0

问题1很容易利用服务器上的Accounts.onCreateUser回调解决 - 它提供了用户对象的回调,这样你就可以只取该用户ID和插入一个新的“我的资料”与该店主。

正如Will Parker指出的那样,虽然用户文档实际上并没有_id,但您需要创建一个并将其添加到该回调返回的user对象。

user._id = Meteor.users._makeNewID(); 
+0

嗨richsilv,实际上当onCreateUser被调用时,用户尚未创建,所以他们没有身份证,你可以用作外键。您当然可以在用户的​​配置文件对象中添加用户的配置文件数据。通过在返回用户之前更改user.profile对象。 – 2014-10-22 10:28:16

+1

啊,是的,好点。答复修正。 – richsilv 2014-10-22 10:31:58

0

Q2:你可以做到这一点,你必须发送电子邮件,密码和配置文件对象服务器,并使用相同的Accounts.createUser。一切正常。