gpt4 book ai didi

javascript - 如何在 Meteor JS 中通过 ID 选择用户?

转载 作者:行者123 更新时间:2023-12-03 05:07:40 25 4
gpt4 key购买 nike

我正在尝试使用 MeteorJS 显示用户个人资料

每个用户都有一个存储在 MongoDB 中的个人资料,但在导航器控制台中它只向我显示用户名、电子邮件和 _id 字段。

这是我的代码:在 /lib/Router.js

Router.route('/profile',{
name : "profile",
data : function(){
user = Meteor.users.find(Meteor.userId()).forEach(function(user) {
console.log(user);
});
//console.log(user);
},
waitOn : function(){
return Meteor.subscribe("allUsers");
}
});

/server/Publications.js:

Meteor.publish("allUsers",function(){
return Meteor.users.find({},{
fields :{
username : 1,
emails : 1,
profile : 1
}
});
});

最佳答案

您的个人资料路线看起来有点奇怪。在我看来,您只需要当前用户的个人资料,而不是所有用户。我会这样写:

Router.route('/profile',{
name : "profile",
data(){
return Meteor.user();
},
waitOn(){
return Meteor.subscribe("me");
}
});

/server/Publications.js:

Meteor.publish("me",function(){
return Meteor.users.find(this.userId,{
fields :{
username : 1,
emails : 1,
profile : 1
}
});
});

关于javascript - 如何在 Meteor JS 中通过 ID 选择用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41958359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com