gpt4 book ai didi

json - 在 Meteor 的服务器上评估数据的最佳方法是什么

转载 作者:可可西里 更新时间:2023-11-01 09:20:56 24 4
gpt4 key购买 nike

我在服务器端创建了一些统计数据。它是根据存储在 MongoDB 中的数据计算一次。它不是 react 性数据。

它以 JSON 格式存储,应发送给客户端以通过 nvd3 在屏幕上显示。最好的方法是什么?

也许这个计算应该在客户端完成?

例如:用数据创建一个数组:

d1 = [['2013-11-01', 123],['2013-11-02', 54],['2013-11-03', 98]];

和第二个数组:

d2 = [['2013-11-01', 432.99],['2013-11-02', 65.99],['2013-11-03', 23.54]];

两个数组都显示在一张图表上。

在服务器端用什么方式生成(Meteor.methods,publish)数据,如何从服务器发送到客户端(Meteor.call,subscribe,HTTP.call)?

如果有任何想法和建议,我将不胜感激。

最佳答案

有 3 种方法可以做到这一点。您可以使用发布/订阅生成数据,使用方法/调用或在客户端上计算它。方法擅长传递短数据集。发布可以更好地处理更大的数据集。

我建议在客户端计算它,因为您可以进行实时更改(排序更改、字段类型/重新缩放等)。你可以使用 Transform计算您的数据。

使用发布/订阅

当你有更大的数据集时这是最好的,因为数据可以流式传输到客户端而不是一大块发送

服务器端

Meteor.publish("data", function(){

var d1 = [['2013-11-01', 123],['2013-11-02', 54],['2013-11-03', 98]];
var d2 = [['2013-11-01', 432.99],['2013-11-02', 65.99],['2013-11-03', 23.54]];

this.added("data", "1", {data: d1 });
this.added("data", "2", {data: d2 });
this.ready();
});

客户端:

Chartdata = new Meteor.Collection("data");
Meteor.subscribe("data");

data = Chartdata.find().fetch()
//Data is stored in an array

使用 Meteor.methods

也许更简单,但它对于较大的数据集不是很好,因为数据是一次性出现的并且不是流式传输的:

服务器:

Meteor.methods({
getData:function() {
var d1 = [['2013-11-01', 123],['2013-11-02', 54],['2013-11-03', 98]];
var d2 = [['2013-11-01', 432.99],['2013-11-02', 65.99],['2013-11-03', 23.54]];

return [d1, d2]
}
});

客户端:

Meteor.call("getData", function(err, result) {
console.log(result)
//Gives back an array with d1 in position 0 and d2 in position 1
});

第三个选项——计算客户端

这取决于您的喜好。如果您更喜欢以这种方式进行操作,那么这是最好的选择。问题是您必须将原始数据发布给客户端以供其计算。

var Data = new Meteor.Collection("data");

//Use a transform to calculate your data
var transform = function(doc) {

//Calculate your value ? Not sure how you're doing this just an example with static data
var data = [['2013-11-01', 123],['2013-11-02', 54],['2013-11-03', 98]];

return data;
}

//result should now have the calculated data
var result = Data.find({}, { transform: transform }).fetch();

关于json - 在 Meteor 的服务器上评估数据的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20403974/

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