2014-10-22 86 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。一切正常。