簡體   English   中英

如何顯示此收藏集?

[英]How do I display this collection?

我正在嘗試創建個人資料頁面,並且需要向模板顯示個人資料名稱和個人簡介。 問題是我無法獲取每個配置文件對象的ID。 如果我可以像使用postId在書中那樣獲得每個配置文件的ID,請執行以下操作。 在這里,下面的代碼是我認為可行但無法實現的方式。 如果您告訴我如何獲取ID,那將非常感謝。

Profile = new Meteor.Collection('profile');

Profile.allow({
    update: ownsDocument
})

Profile.deny({
  update: function(profileId, profile, fieldNames) {
    return (_.without(fieldNames, 'bio').length > 0);
  }
});


Accounts.onCreateUser(function(options, user){
    Meteor.methods({
        profile: function(postAttributes) {
    var user = Meteor.user();
    var profile = _.extend(_.pick(options.profile, 'bio'), {
        userId: user._id, 
        profilename: user.username, 
        submitted: new Date().getTime(),
        postsCount: 0, posts : []
    });


    var profileId = Profile.insert(profile);

    return profileId;
    }

   });
    return user;
});

在發現流星示例中,他們使用一種方法來插入Post ,然后返回其ID。 在您的情況下,新的Profile將插入到異步回調中,因此您無法返回ID。 但是,您知道userId ,因此可以使用它來獲取配置文件。

服務器

Accounts.onCreateUser(function(options, user){

    var user = Meteor.user();
    var profile = _.extend(_.pick(options.profile, 'bio'), {
        userId: user._id, 
        profilename: user.username, 
        submitted: new Date().getTime(),
        postsCount: 0,
        posts : []
    });

    Profile.insert(profile);

    return user;
});

客戶

Template.profile.profile = function () {
  return Profiles.findOne({userId: Meteor.userId()});
};

您似乎對某些“流星”想法有些困惑。

首先,Meteor.methods({...})是可以使用Meteor.call({})客戶端調用的函數

它們應該出現在頂層,而不應該出現在Accounts.onCreateUser之類的其他函數中

對於這個用例,我什至看不到為什么需要一個方法。 您要做的就是檢索要存儲在將要發送到客戶端的數據中的數據。

如果您使用基於帳戶的程序包,則將自動獲取一個Meteor.users集合,該集合適合您的工作。 我認為沒有必要收集個人資料

這是一個說明它的流星板: http ://meteorpad.com/pad/xL9C8eMpcwGs553YD

注意我將簡歷存儲在用戶的個人資料部分。 這是因為默認情況下,用戶可以編輯自己的個人資料部分,因此我可以在Meteor.users.update(...)客戶端進行操作。

這只是顯示一些概念的示例。 它確實有不好的做法。 首先,我建議添加軟件包account-ui並使用{{> loginButtons}}幫助器。 它為您提供錯誤檢查等。 我之所以不使用它,是因為我想展示如何在創建帳戶之前允許用戶輸入他們的個人資料,以及如何在Accounts.onCreateUser中使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM