Javascript Meteor如何检索集合对象的属性值
我有Meteor.users收藏。用户有一个名为“LastQuestionResponsed”的属性,该属性是用户回答的最后一个问题的questionIdJavascript Meteor如何检索集合对象的属性值,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我有Meteor.users收藏。用户有一个名为“LastQuestionResponsed”的属性,该属性是用户回答的最后一个问题的questionId { "_id" : "jrCJiuo7eprNFKfLG", "name" : "Elon Musk", "lastQuestionAnswered" : 1 } 如何将(当前用户的)LastQuestionResponsed属性的值提取到变量中?所以最后我们有 Var lastAnsweredQuestionId = ??? 我正在尝试
{ "_id" : "jrCJiuo7eprNFKfLG",
"name" : "Elon Musk",
"lastQuestionAnswered" : 1 }
如何将(当前用户的)LastQuestionResponsed属性的值提取到变量中?所以最后我们有
Var lastAnsweredQuestionId = ???
我正在尝试在服务器端执行此操作。我看过相关的问题,但找不到任何提示。提前感谢您的帮助。为了在客户端获得当前用户,您需要使用
Meteor.user()
方法。因此,您的代码将如下所示:
var lastAnsweredQuestionId = Meteor.user().lastQuestionAnswered;
它将在客户端或服务器端的方法中工作(如果用户已登录)
更新
如果您需要在publish函数中获得相同的结果,则需要一种稍微不同的方法,因为Meteor.user()
在发布服务器中不起作用:
var currentUser = Meteor.users.findOne({_id: this.userId});
var lastAnsweredQuestionId = currentUser.lastQuestionAnswered;
我不确定你到底需要实现什么。上次回答的问题是什么意思?用户是否回答了某种类型的问题ID?我们已经有
Meteor.users
。User
集合应该存储什么?对不起,这是Meteor.users集合。LastQuestionResponsed只是用户回答的最后一个问题的questionID。看起来不错。我需要在发布函数中使用它。它说我不能在发布函数中使用Meteor.user。有什么建议吗?由于某些原因,它仍然不起作用:Meteor.publish('questions',function(){var currentUser=Meteor.users.findOne({u id:this.userId});var lastQuestionAnsweredId=currentUser.lastQuestionResponsed;return questions.find({number:lastQuestionAnsweredId});})代码>抱歉,我不知道如何在评论中换行此发布方法应该发布屏幕的当前标题。它就是不出现。我以后会在Mac上试用,可能只是Windows兼容性问题,以前有过:)谢谢你的帮助,会回来找你的。或者你有其他想法吗?我在Mac上试过,但不幸的是它仍然不起作用。你知道我在哪里可以找到答案的其他提示吗?非常感谢您的帮助。