【发布时间】:2023-03-15 08:27:01
【问题描述】:
我有以下模板,当用户单击特定帖子上的“谁发布这个”按钮时加载该模板。
<template name="profile">
{{#if existinguser}}
`//other code to render when esitinguser returns true//`
{{else}}
<p>This user doesn't exist.</p>
{{/if}}
</template>
这是现有的用户助手:
'existinguser': function() {
if (Meteor.users.find({
"profile.username": document.URL.split('/')[4]
}).count() > 0) {
return true;
} else {
return false;
}
}
还有 route.js
Router.route('/user/:username', {
name: 'profile',
loadingTemplate: 'loading',
waitOn:function(){Meteor.subscribe('users');
},
data: function() {
return Meteor.users.find({
'profile.username': this.params.username
});
}
});
现在,当点击链接时,else 块被渲染并且页面显示这个用户不存在,即使它在用户集合中。
如果我在同一个 url 上重新加载页面,它会呈现并显示 if 块中的内容。
可能是什么问题?
【问题讨论】:
-
你可以使用
Router.current().params.username而不是document.URL.split('/')[4],那么你正在使用的有点hacky -
路由器功能会在流星帮助 js 文件中工作吗?
-
你为什么不试试呢?
-
你试过我的回答并在 Meteor.subscribe 调用前面加上“return”吗?
标签: javascript html templates meteor