gpt4 book ai didi

javascript - 如何在 Meteor 中的 nvd3 的 Template.rendered 中访问订阅

转载 作者:行者123 更新时间:2023-12-03 07:12:23 25 4
gpt4 key购买 nike

我想用简单的 nvd3 discrete bar chart 显示集合中的数据.

当我尝试使用本地集合时,效果很好。现在我将相同的数据转移到数据库集合中,但我无法在 Meteor 的 .rendered 中获取数据。

Template.chartPopularWordsAll.onCreated(() => {
let template = Template.instance();
template.autorun(() => {
template.subscribe('dataViewed'); // DataViewed.find()
});

Template.chartPopularWordsAll.rendered = function() {
let data = DataViewed.find({}, {
limit: 5,
sort: {
timesViewed: -1
}
}).fetch();

console.log(data); // <-- this returns an empty array
}

问题:我如何访问 .rendered 中的数据?

在 Meteor 文档中搜索“.rendered”没有给出任何结果,我只能找到 .onRendered。 .rendered 是最新的还是已弃用?

提前致谢!

莫夫

最佳答案

我认为这里的问题是混合了订阅和获取的自动运行:

自动运行是在数据更改时运行的,因此不需要在自动运行中进行订阅,而是需要进行数据查找。

试试这个:

Template.chartPopularWordsAll.onCreated(() => {
let template = Template.instance();
template.subscribe('dataViewed'); // DataViewed.find()
});

Template.chartPopularWordsAll.rendered = function() {
template.autorun(() => {
let data = DataViewed.find({}, {
limit: 5,
sort: {
timesViewed: -1
}
}).fetch();

console.log(data); //
}
}

如果这不起作用,请尝试不获取对数据的调用,而是在需要数据时获取:Collection.find() 为您提供一个游标,它是 react 性的,但是一旦获取,您就会得到一个数组,这不是 react 性的。 Collection.find() 部分“应该”在自动运行中 react ,但我不能 100% 确定。

关于javascript - 如何在 Meteor 中的 nvd3 的 Template.rendered 中访问订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36580238/

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