gpt4 book ai didi

javascript - 在 Meteor.user 字段中保存了对象的 id,在模板中调用了 id,如何显示对象的数据而不是 id?

转载 作者:行者123 更新时间:2023-12-03 06:28:19 28 4
gpt4 key购买 nike

我在列表中有一个名为类别的对象集合。在我的 html 中,每个类别对象都是一个列表项,并且该类别对象中还有其他对象,这些对象是类别 id、名称和属于该类别的帖子数组。看图片。

enter image description here

每个类别列表项上都有一个用户可以单击的按钮,然后将类别 ID 保存在名为 name 的 Meteor.user 字段中。

<template name="CategoriesMain">
<ul>
{{#each articles}}
<li>
<a href="/catsingle/CategorySingle/{{_id}}"><h2>{{name}}</h2></a>
<button type="button" class="toggle-category">Add to User Field</button>
</li>
{{/each}}
</ul>
</template>

为了实现这一点,我在我的助手中有这个

Template.CategoriesMain.events({
'click .toggle-category': function(e){
//take the category id on click
var ob = this._id;
//make it into array
var id = $.makeArray( ob );
e.preventDefault();
Meteor.call('addingCategory', id, function(error, user){ console.log(id)});
},
});

然后用我的方法。

Meteor.methods({
addingCategory: function(name) {
Meteor.users.update({
_id: Meteor.userId()
},
{
$addToSet: {

name: name
}
});
}
});

as you can see, the category ids are being inputed into the user data as an array

每个用户都有一个时间线,其中会显示已保存的类别 ID。

<template name="userTimeline">
{{#if currentUser}}
<div class="timeline-user">
<p>{{name}}</p>
</div>
{{/if}}
</template>

enter image description here

在助手中

Template.userTimeline.helpers({

name: function() {
return Meteor.user().name;

//this is what im experiment with to link the category id's in the timeline with the category ids from the Category collection but i'm not sure if im going the right direction.
var name = FlowRouter.getParam('_id')
return CategoryCollection.find({_id: id}).fetch();
}
});

我的问题是,不是显示类别 id,而是可以以某种方式获取这些类别 id 的对象,即属于该类别的帖子?我知道我必须以某种方式将用户收集的 id 与类别的 id 链接起来

EDIT

我修改了我的 meteor 方法,将“类别”声明为数组,如下所示:

Accounts.onCreateUser(function(options, user){
user.category = [];
return user;
});


Meteor.methods({
addingCategory: function(category) {
console.log(Meteor.userId());
Meteor.users.update({
_id: Meteor.userId()
},
{
$addToSet: {
category: category
}
});
}
});

这就是 Meteor.users 的样子,看看“类别”字段。

enter image description here

我在我的助手中修改了以下内容,这引发了错误:

Template.userTimeline.helpers({

category(){
return CategoryCollection.find({ _id: { $in: this.category }}); // a cursor of categories
}
});

其中 CategoryCollection 是保存类别的集合

由于我在方法中将“category”声明为数组,因此我不再像以前那样将每个类别 id 更改为数组,因此我将模板事件更改为此。

Template.CategoriesMain.events({
'click .toggle-category': function(e){
var ob = this._id;
console.log(ob);
e.preventDefault();
Meteor.call('addingCategory', ob, function(error, user){ console.log(ob)});
}
});

我的发布已更改为:我不知道这里是否应该使用 $in ?

Meteor.publish(null, function() {
return Meteor.users.find({_id: this.userId}, {fields: {category: 1}});
});

我的html已更改为:

<template name="userTimeline">
{{#if currentUser}}
<div class="timeline-user">
{{#each category}}
Count: {{count}}
Description: {{description}}
Link: {{link}}
{{#each posts}}
ID: {{id}}
Title: {{title}}
{{/each}}
{{/each}}
</div>
{{/if}}
</template>

最佳答案

您可以通过迭代类别 ID 数组并使用帮助器返回整个类别对象来显示与用户相关的类别。然后您可以在其中循环浏览帖子。

<template name="userTimeline">
{{#if currentUser}}
<div class="timeline-user">
{{#each categories}}
Count: {{count}}
Description: {{description}}
Link: {{link}}
{{#each posts}}
ID: {{id}}
Title" {{title}}
{{/each}}
{{/each}
</div>
{{/if}}
</template>

然后你的 helper :

template.userTimeline.helpers({
categories(){
if ( this.category &&
typeof(this.category) === "object" &&
this.category.length ){ // check to see if the list of categories is an array
return categories.find({ _id: { $in: this.category }}); // a cursor of categories
} else {
console.log(this.category);
return null;
}
}
});

请记住,userTimeline 模板的数据上下文 thisMeteor.user() 对象,因此 this.name 将是 Meteor.user().name

中的类别 id 数组

关于javascript - 在 Meteor.user 字段中保存了对象的 id,在模板中调用了 id,如何显示对象的数据而不是 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38544870/

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