,我不確定在哪裏可以找到這個文檔。我如何添加更多的對象到用戶對象?MeteorJS/Mongodb的關係如何?
舉例來說,如果我跑
meteor add accounts
,我得到一個完整的用戶集合與工作用戶登錄/註冊模板。我想在此用戶集合中添加帖子集合/對象,以便用戶只能查看自己的帖子。
那麼如何將每個帖子添加到當前用戶對象?
,我不確定在哪裏可以找到這個文檔。我如何添加更多的對象到用戶對象?MeteorJS/Mongodb的關係如何?
舉例來說,如果我跑
meteor add accounts
,我得到一個完整的用戶集合與工作用戶登錄/註冊模板。我想在此用戶集合中添加帖子集合/對象,以便用戶只能查看自己的帖子。
那麼如何將每個帖子添加到當前用戶對象?
您可以通過添加accounts-password
軟件包將用戶添加到Meteor.users集合中。使用Accounts.createUser()
方法創建新用戶。
在這裏找到的文檔:https://docs.meteor.com/api/passwords.html#Accounts-createUser
Meteor.users是一個句柄用戶收集流星。您可以像使用其他任何系列一樣使用它AKA
Meteor.users.findOne(id)
or
Meteor.users.update(...)
當然,您不能將posts集合添加到用戶集合中。這些將是不同的集合。根據收集的用戶文檔中的MongoDB
存儲對象是非常簡單的:
Meteor.users.update(
{ _id: userId },
{ $set: { objectFieldName: { a: 1, b: 2 }}}
)
或者,如果你需要做的是在用戶創建你應該用戶Accounts package hooks。
您正在接近它錯誤。使用pub/subs來實現這一點。
當您插入後,有一個字段名爲用戶id或OWNERID
//inside Meteor.methods() on server side
Posts.insert({
owner: Meteor.userId(),
//some other fields
});
在出版物
然後,用戶擁有
//publication on server side
//checks if the visitor is a user
//if user, returns that user's posts
Meteor.publish('posts', function() {
if (this.userId) {
return Posts.find({owner: this.userId})
}
});
然後訂閱刊物只返回的帖子。不需要參數:
//client side
Meteor.subscribe('posts')
所以在我的Template.body.helpers,我有一個方法稱爲posts(){}我怎樣才能返回與該特定用戶相關的帖子? –
@MkrJs請閱讀一些[文檔](https://docs.meteor.com/)和[流星指南](https://guide.meteor.com/)!你一定會找到你正在尋找的信息。 –