How can I create a posts array on Users accounts so users are only posting to their own account


#1

So I’m trying to create a posts array for each user so that each user would only post to their account and they can only view their own posts, this would look something like:
Tom = {
_id : 21r3qtg4r3wfe,
posts: [
{title: ‘life is cool’, content: ‘foobar’}, {title: ‘life is very cool’, content: ‘foobar foo’}
]
}

Joe = {
_id : 4355934294234,
posts: [
{title: ‘life is bad’, content: ‘barfoo’}, {title: ‘life is not so cool’, content: ‘barfoo bar’}
]
}

In my imports/methods.js I have:

Meteor.users.Posts = [];

Meteor.methods({
insertPost: function(userId,title,content){
Meteor.users.update(
{
_id: userId
},{
$push:{
Post:
{
title,
content
}
}
}
)
}
});

and in my template.body.helpers:

posts(){

return return Meteor.users.Posts
}

I’m sure I structured this wrong because I’m not getting anything back and I’m not sure how to fix it. Any help would be appreciated


#2

Hi there.

Conventionally with mongodb, you would create a separate Posts collection and place the add the userId on a document when it is created. When you want to retrieve the post, you would query the posts collection with the current userId.