gpt4 book ai didi

javascript - 从数据为 :function in iron:router Meteor 的两个集合返回数据

转载 作者:行者123 更新时间:2023-11-30 00:17:30 26 4
gpt4 key购买 nike

不使用 publish 或 waitOn 或其他用于学习目的的高级技术。不知何故我无法让它工作。

Router.route('/browse/:_id', function(){
this.render('navigation', {to: 'navbar'});
this.render('header', {to: 'header'});
this.render('website', {
to: 'main',
data: function(){
return {
websites: Websites.findOne({_id: this.params._id}),
comments: Comments.find({siteId: this.params._id})
}
}
});
});

此代码非常适合“评论”,而“网站”不会呈现。
只有当我像这样返回时,“网站”下的字段才会以 HTML 呈现:

data: function(){   /*This works for websites but if used on comments it won't*/
return Websites.findOne({_id: this.params._id})
}

而对于“评论”,它仅在我以对象表示法或键值对给出时才有效:

data: function(){   /*This works for comments but if used on websites it wont*/
return {comments: Comments.find({siteId: this.params._id})}
}

我的问题是我需要从同一模板中的两个集合中获取数据。非常欢迎任何建议。谢谢!

编辑 包括我的 HTML 模板:“网站”模板

<template name="website">
<div class="container">
/*solved by including #with websites here*/
<div class="website">
<div class="website-header js-url">
<h4>{{title}}</h4>
</div>
<div class="website-body">
<tr><td>URL:</td> <td>{{url}}</td></tr>
<tr><td>Description</td><td>{{description}}</td></tr>

<div class="votes">
<div class="upvote_row">
<tr>
<td>Up Votes:</td>
<td>{{upVote.length}}<a href="#" class="btn btn-default js-upvote"><span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span></a></td>
</tr>
</div>
<div class="downvote_row">
<tr>
<td>Down Votes:</td>
<td>{{downVote.length}}<a href="#" class="btn btn-default js-downvote"><span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span></a></td>
</tr>
</div>
</div>

<tr><td>Posted By:</td><td><a href class="">{{createdBy}}</a> on {{createdOn.toDateString}} at {{createdOn.toLocaleTimeString}} </td></tr>
</div>
</div>
/* /with */
<div class="comments">
<h5>Comment Section</h5>
{{#if currentUser}}
<a class="btn btn-default js-toggle-comment-form" href>
<span class="glyphicon glyphicon-comment" aria-hidden="true"></span>
</a>
{{/if}}
<div id="comment_form" class="hidden_div">
<form class="js-save-comment-form">
<div class="form-group">
<label for="user_comment">Add Comment</label>
<input type="textfield" class="form-control" id="user_comment" placeholder="Your views on this website" required>
</div>

<button type="submit" class="btn btn-default">Submit</button>
<button class="btn btn-default js-cancel-submit">Cancel</button>
</form>
</div>

<ol>
{{#each comments}}
{{>comment_item}}
{{/each}}
</ol>
</div>

</div>
</template>

然后我的评论模板完美运行。

<template name="comment_item">
<li class="card-panel teal darken-1 comments">

<blockquote>
<p class='card-content'>
{{userComment}}
</p>
<footer>
<a href="{{url}}">{{postedBy}}</a> on {{postedOn.toDateString}} at {{postedOn.toLocaleTimeString}}
<a href="#" class="btn btn-default js-upvote">
<span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>
<span class="badge badge-notify">{{upVote.length}}</span>
</a>
<a href="#" class="btn btn-default js-downvote">
<span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span>
<span class="badge badge-notify">{{downVote.length}}</span>
</a>
</footer>
</blockquote>
</li>
</template>

最佳答案

您还应该显示您的 HTML 模板代码。我怀疑您是在直接引用网站对象的键,而不是使用 {{websites.keyname}}。根据您的数据功能,您的数据上下文将是 {websites: an object, comments: a cursor} 如果您尝试从您的网站访问 {{keyname}},您什么也得不到,因为您的数据上下文没有名为 keyname 的顶级键。

关于javascript - 从数据为 :function in iron:router Meteor 的两个集合返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34248007/

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