雅虎财经、Meteor 和会议
Yahoo Finance, Meteor and sessions
我正在尝试使用 Meteor 从 yahoo finance 输出股票信息。它以前工作过,但我已经离开这个项目一段时间了。我已经更新了流星,所以我认为在更新过程中可能有什么东西被破坏了。这是我正在使用的包。
https://atmospherejs.com/ajbarry/yahoo-finance
我成功从yahoo抓取了数据,但它似乎没有传递到session,我无法在模板中输出objects数据。
这是我的客户 javascript 代码。
Template.stock.rendered = function (){
// if ( _.isEmpty(Session.get('ENW.V')) ) {
Meteor.call('getQuote', 'ENW.V', function(err, result) {
Session.set('ENW.V', result['ENW.V']);
console.log(result);
});
// }
if ( _.isEmpty(Session.get('E4U.F')) ) {
Meteor.call('getQuote', 'E4U.F', function(err, result) {
Session.set('E4U.F', result['E4U.F']);
console.log(result);
});
}
};
Template.stock.helpers({
stock: function() {
return Session.get('ENW.V');
},
stock2: function() {
return Session.get('E4U.F');
}
});
这是库存 html 模板
<template name="stock">
<div class="container">
{{> header}}
</div>
{{>investorsSubNav}}
{{> sidebar}}
<div class="investorsWrap">
<div class="investorsBody">
<section class="stockBodyHero">
<div class="stockLeft">
<div class="stockCenter">
<h2>Enwave Corporation TSX</h2>
<ul>
<li><strong>Symbol</strong>{{stock.symbol}}</li>
<li><strong>Market Cap</strong> $ {{stock.marketCapitalization}}</li>
<li><strong>Average Daily Volume</strong> $ {{stock.averageDailyVolume}}</li>
<li><strong>52 Week Low</strong> $ {{stock.[52WeekLow]}}</li>
<li><strong>52 Week High</strong> $ {{stock.[52WeekHigh]}}</li>
<li><strong>Change</strong> {{stock.change}}</li>
</ul>
</div>
</div>
<div class="stockRight">
<div class="stockCenter">
<h2>Enwave Corporation FSE</h2>
<ul>
<li><strong>Symbol</strong> {{stock2.symbol}} </li>
<li><strong>Market Cap</strong> $ {{stock2.marketCapitalization}}</li>
<li><strong>Average Daily Volume</strong> $ {{stock2.averageDailyVolume}}</li>
<li><strong>52 Week Low</strong> $ {{stock2.[52WeekLow]}}</li>
<li><strong>52 Week High</strong> $ {{stock2.[52WeekHigh]}}</li>
</ul>
</div>
</div>
</section>
</div>
</div>
</template>
这是服务器端方法
Meteor.methods({
getQuote: function( stockname ) {
return YahooFinance.snapshot({symbols: [stockname] , fields:['n','a','b','j1','a2','k','j','c1'] });
}
});
这是控制台记录时 object 的格式
好的,我想我明白了问题所在。让我们关注 ENW.V:
您正在设置:
Session.set('ENW.V', result['ENW.V']);
你的 console.log(result)
正在返回:
stock.js:5 [Object]0:
Object
52WeekHigh: 1.34
52WeekLow: 0.69
ask: 0.89
averageDailyVolume: 77643
bid: 0.81
change: 0.04
marketCapitalization: "71.84M"
name: "ENWAVE CORP"
symbol: "ENW.V"
__proto__: Object
length: 1
__proto__: Array[0]
如果输出正确,则没有 ENW.V
键,result[ENW.V']
将是未定义的。
Yahoo 正在返回长度为 1 的对象数组。我建议对您的助手和模板进行以下更改:
Template.stock.rendered = function (){
Meteor.call('getQuote', 'ENW.V', function(err, result) {
Session.set('ENW.V', result[0]); // extract the first element of the array
console.log(result);
console.log(Session.get('ENW.V')); // double check
});
Meteor.call('getQuote', 'E4U.F', function(err, result) {
Session.set('E4U.F', result[0]);
console.log(result);
}
Template.stock.helpers({
stock: function(symbol) { // this avoids having to create one helper/symbol
return Session.get(symbol);
}
});
html:
<template name="stock">
<div class="investorsWrap">
<div class="investorsBody">
<section class="stockBodyHero">
<div class="stockLeft">
{{#with stock 'ENW.V'}}
{{> oneStock}}
{{/with}}
</div>
<div class="stockRight">
{{#with stock 'E4U.F'}}
{{> oneStock}}
{{/with}}
</div>
</section>
</div>
</div>
<template name="oneStock">
<div class="stockCenter">
<h2>{{name}}</h2>
<ul>
<li><strong>Symbol</strong>{{symbol}}</li>
<li><strong>Market Cap</strong> $ {{marketCapitalization}}</li>
<li><strong>Average Daily Volume</strong> $ {{averageDailyVolume}}</li>
<li><strong>52 Week Low</strong> $ {{this.[52WeekLow]}}</li>
<li><strong>52 Week High</strong> $ {{this.[52WeekHigh]}}</li>
<li><strong>Change</strong> {{change}}</li>
</ul>
</div>
</template>
我正在尝试使用 Meteor 从 yahoo finance 输出股票信息。它以前工作过,但我已经离开这个项目一段时间了。我已经更新了流星,所以我认为在更新过程中可能有什么东西被破坏了。这是我正在使用的包。 https://atmospherejs.com/ajbarry/yahoo-finance
我成功从yahoo抓取了数据,但它似乎没有传递到session,我无法在模板中输出objects数据。
这是我的客户 javascript 代码。
Template.stock.rendered = function (){
// if ( _.isEmpty(Session.get('ENW.V')) ) {
Meteor.call('getQuote', 'ENW.V', function(err, result) {
Session.set('ENW.V', result['ENW.V']);
console.log(result);
});
// }
if ( _.isEmpty(Session.get('E4U.F')) ) {
Meteor.call('getQuote', 'E4U.F', function(err, result) {
Session.set('E4U.F', result['E4U.F']);
console.log(result);
});
}
};
Template.stock.helpers({
stock: function() {
return Session.get('ENW.V');
},
stock2: function() {
return Session.get('E4U.F');
}
});
这是库存 html 模板
<template name="stock">
<div class="container">
{{> header}}
</div>
{{>investorsSubNav}}
{{> sidebar}}
<div class="investorsWrap">
<div class="investorsBody">
<section class="stockBodyHero">
<div class="stockLeft">
<div class="stockCenter">
<h2>Enwave Corporation TSX</h2>
<ul>
<li><strong>Symbol</strong>{{stock.symbol}}</li>
<li><strong>Market Cap</strong> $ {{stock.marketCapitalization}}</li>
<li><strong>Average Daily Volume</strong> $ {{stock.averageDailyVolume}}</li>
<li><strong>52 Week Low</strong> $ {{stock.[52WeekLow]}}</li>
<li><strong>52 Week High</strong> $ {{stock.[52WeekHigh]}}</li>
<li><strong>Change</strong> {{stock.change}}</li>
</ul>
</div>
</div>
<div class="stockRight">
<div class="stockCenter">
<h2>Enwave Corporation FSE</h2>
<ul>
<li><strong>Symbol</strong> {{stock2.symbol}} </li>
<li><strong>Market Cap</strong> $ {{stock2.marketCapitalization}}</li>
<li><strong>Average Daily Volume</strong> $ {{stock2.averageDailyVolume}}</li>
<li><strong>52 Week Low</strong> $ {{stock2.[52WeekLow]}}</li>
<li><strong>52 Week High</strong> $ {{stock2.[52WeekHigh]}}</li>
</ul>
</div>
</div>
</section>
</div>
</div>
</template>
这是服务器端方法
Meteor.methods({
getQuote: function( stockname ) {
return YahooFinance.snapshot({symbols: [stockname] , fields:['n','a','b','j1','a2','k','j','c1'] });
}
});
这是控制台记录时 object 的格式
好的,我想我明白了问题所在。让我们关注 ENW.V:
您正在设置:
Session.set('ENW.V', result['ENW.V']);
你的 console.log(result)
正在返回:
stock.js:5 [Object]0:
Object
52WeekHigh: 1.34
52WeekLow: 0.69
ask: 0.89
averageDailyVolume: 77643
bid: 0.81
change: 0.04
marketCapitalization: "71.84M"
name: "ENWAVE CORP"
symbol: "ENW.V"
__proto__: Object
length: 1
__proto__: Array[0]
如果输出正确,则没有 ENW.V
键,result[ENW.V']
将是未定义的。
Yahoo 正在返回长度为 1 的对象数组。我建议对您的助手和模板进行以下更改:
Template.stock.rendered = function (){
Meteor.call('getQuote', 'ENW.V', function(err, result) {
Session.set('ENW.V', result[0]); // extract the first element of the array
console.log(result);
console.log(Session.get('ENW.V')); // double check
});
Meteor.call('getQuote', 'E4U.F', function(err, result) {
Session.set('E4U.F', result[0]);
console.log(result);
}
Template.stock.helpers({
stock: function(symbol) { // this avoids having to create one helper/symbol
return Session.get(symbol);
}
});
html:
<template name="stock">
<div class="investorsWrap">
<div class="investorsBody">
<section class="stockBodyHero">
<div class="stockLeft">
{{#with stock 'ENW.V'}}
{{> oneStock}}
{{/with}}
</div>
<div class="stockRight">
{{#with stock 'E4U.F'}}
{{> oneStock}}
{{/with}}
</div>
</section>
</div>
</div>
<template name="oneStock">
<div class="stockCenter">
<h2>{{name}}</h2>
<ul>
<li><strong>Symbol</strong>{{symbol}}</li>
<li><strong>Market Cap</strong> $ {{marketCapitalization}}</li>
<li><strong>Average Daily Volume</strong> $ {{averageDailyVolume}}</li>
<li><strong>52 Week Low</strong> $ {{this.[52WeekLow]}}</li>
<li><strong>52 Week High</strong> $ {{this.[52WeekHigh]}}</li>
<li><strong>Change</strong> {{change}}</li>
</ul>
</div>
</template>