流星返回值作为字符串 [英] Meteor return value as string
问题描述
这就是我想要达到的目标.我有两个集合:Questions
和 Answers
.当用户回答问题时,答案将设置在 Answers
集合中,并随其传递问题的 ID.
我正在使用此帮助程序显示包含所有问题的列表:
问题:函数(){返回 Question.find({});}
还有这个 html:
{{#每个问题}}<li>{{question}}</li>{{/每个}}</ol>
我想在每个问题下方显示每个用户给出的答案.我想将问题的 ID 作为字符串值返回,因此我可以执行以下操作:
answers:function(){return Answer.find({问题:<问题的ID>});}
谁能帮我解决这个问题?
你的,L
假设您的 Question
集合具有以下架构(为简洁起见进行了简化):
QuestionSchema = new SimpleSchema({标题: {类型:字符串,标签:问题"},类别: {类型:字符串,标签:类别"}});
并且您的 Answer
集合有
AnswerSchema = new SimpleSchema({文本: {类型:字符串,标签:问题"},作者: {类型:字符串,标签:作者"}问题: {类型:字符串,标签:问题"}});
您可以通过创建两个模板助手来解决这个问题,其中第一个只返回问题文档数组,第二个将单个问题 ID 作为参数并返回具有该问题 ID 的所有答案的游标:
Template.questions.helpers({问题:函数(){返回 Question.find({}).fetch();},答案:功能(问题ID){返回 Answer.find({question: questionId}).fetch();}});
接下来模板需要嵌套的 {{#each}}
块,第一个块遍历问题数组并将答案作为下一个助手的参数传递给下一个.
{{#每个问题}}<h1>{{this.title}}</h1><ol>{{#每个人都回答这个._id}}<li>{{text}}</li>{{/每个}}</ol>{{/每个}}模板>
This is what I want to achieve.
I got two collections: Questions
and Answers
.
When a user answers a question, the answer will set in the Answers
collection, and it passes the ID of the question with it.
I'm displaying a list with all the questions using this helper:
questions: function (){
return Question.find({});
}
And this html:
<ol>
{{#each questions}}
<li>{{question}}</li>
{{/each}}
</ol>
I want to display the answers that every user gave, below each question. I want to return the Question's ID as a string value, so I can do something like:
answers:function(){
return Answer.find({question: <The ID of the question>});
}
Can anyone help me with this?
Yours, L
Supposing your Question
collection has the following schema (simplified for brevity):
QuestionSchema = new SimpleSchema({
title: {
type: String,
label: "Question"
},
category: {
type: String,
label: "Category"
}
});
and your Answer
collection has
AnswerSchema = new SimpleSchema({
text: {
type: String,
label: "Question"
},
author: {
type: String,
label: "Author"
}
question: {
type: String,
label: "Question"
}
});
You can go about this by creating two template helpers where the first just returns an array of question documents and the second takes a single question id as parameter and returns a cursor of all of the answers with that question id:
Template.questions.helpers({
questions: function(){
return Question.find({}).fetch();
},
answers: function(questionId){
return Answer.find({question: questionId}).fetch();
}
});
Next the template needs nested {{#each}}
blocks with the first one iterating over the questions array and passing the answers to the next each as the parameter of the next helper.
<template name="questions">
{{#each questions}}
<h1>{{this.title}}</h1>
<ol>
{{#each answers this._id}}
<li>{{text}}</li>
{{/each}}
</ol>
{{/each}}
</template>
这篇关于流星返回值作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!