我正在尝试从Yahoo Finance与Meteor输出股票信息。它以前工作过,但我离开该项目已有一段时间了。我已经更新了流星,所以我认为在更新过程中可能有些问题。这是我正在使用的软件包。
https://atmospherejs.com/ajbarry/yahoo-finance

我已经成功地从yahoo抓取了数据,但它似乎没有传递到会话中,因此无法在模板中输出对象数据。

这是我的客户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'] });
  }
});


这是控制台记录对象时的格式
javascript - 雅虎财经, meteor 与 session-LMLPHP

最佳答案

好的,我想我看到了问题。让我们专注于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']将是未定义的。

雅虎将返回长度为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>

09-25 19:35