[英]Yahoo Finance, Meteor and sessions
I am trying to output stock information from yahoo finance with Meteor. 我正在尝试从Yahoo Finance与Meteor输出股票信息。 It was working before but I have been away from the project for a while. 它以前工作过,但我离开该项目已有一段时间了。 I have updated meteor so I am thinking something may have been broken during the update. 我已经更新了流星,所以我认为在更新过程中可能有些问题。 This is the package I am using. 这是我正在使用的软件包。 https://atmospherejs.com/ajbarry/yahoo-finance https://atmospherejs.com/ajbarry/yahoo-finance
I am successfully grabbing the data from yahoo but it doesn't seem to pass into a session and I can't output the objects data in the template. 我已经成功地从yahoo抓取了数据,但它似乎没有传递到会话中,因此无法在模板中输出对象数据。
This is a my client javascript code. 这是我的客户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');
}
});
This is the stock html template 这是库存的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>
this is the server side method 这是服务器端方法
Meteor.methods({
getQuote: function( stockname ) {
return YahooFinance.snapshot({symbols: [stockname] , fields:['n','a','b','j1','a2','k','j','c1'] });
}
});
This is the format of the object when it is console logged 这是控制台记录对象时的格式
ok I think I see the problem. 好的,我想我看到了问题。 Let's focus on ENW.V: 让我们专注于ENW.V:
You're setting: 您正在设置:
Session.set('ENW.V', result['ENW.V']);
And your console.log(result)
is returning: 并且您的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]
If that output is correct then there is no ENW.V
key and result[ENW.V']
will be undefined. 如果该输出正确,则没有ENW.V
键,并且result[ENW.V']
将不确定。
Yahoo is returning an array of objects of length 1. I suggest the following changes to your helpers and templates: 雅虎将返回长度为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: 的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.