2016-03-31 9 views
0

私はMongoでMeteorを使用しています。 私は、次のコード(CoffeeScriptの)とに挿入しています通知の収集、持っている - 基本的に私が何をしたいのかMongo - フィールドが別のコレクション配列にあるコレクションを返す方法

Notifications.new 
     title: Meteor.user().username + ' liked your listing' 
     link: '/listing/' + $(e.currentTarget).attr('doc') 
     icon: 'thumbs-up' 
     class: 'default' 
     target: $(e.currentTarget).attr('doc') 

は、リストを作成したユーザー(「ターゲット」フィールド内に通知を返すですコレクションはリスティングIDです)。

$(e.currentTarget).attr( 'doc')。createdByを挿入しようとしましたが、うまくいきませんでした。 (私はこのことが初めてです)

通知にcreatedByを挿入する方法を見つけることができれば、すべてが良くなりますが、私はそれを行う方法を理解できません。

私の他のアイデアは、このような何かをしようとしてやっていた - 私は私のユーザーコレクション内のリストの配列を持っている*

Meteor.publish 'notifications', -> 
    Notifications.find {**** return Notifications where their target ID is in the current User listing array ****} 

誰もがこのための任意のアイデアがありますか? それほど感謝します。 おかげで

答えて

0

基本的には、最初のリストを検索し、それを作成したユーザーの_idを取得する必要があります:

let listingId = $(e.currentTarget).attr('doc'); 

// assuming that the Listings collection has such a key 
// AND that the Listing referred to is published to the current user 

let createdById = Listings.findOne(listingId).createdById; 

Notifications.new 
    title: Meteor.user().username + ' liked your listing' 
    link: '/listing/' + $(e.currentTarget).attr('doc') 
    icon: 'thumbs-up' 
    class: 'default' 
    target: createdById 

あなたが作成者の_id上場文書内を追跡していないが、代わりにどのようなリストを追跡している場合がユーザー文書に作成された場合は、代わりにコレクションを検索する必要があります。

let createdById = Meteor.users.findOne({listings: listingId})._id; 
関連する問題