Search code examples
javascriptsessionmeteorhelperyahoo-finance

Yahoo Finance, Meteor and sessions


I am trying to output stock information from yahoo finance with 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

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.

This is a my client javascript code.

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

<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 enter image description here


Solution

  • ok I think I see the problem. Let's focus on ENW.V:

    You're setting:

    Session.set('ENW.V', result['ENW.V']);
    

    And your console.log(result) is returning:

    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.

    Yahoo is returning an array of objects of length 1. I suggest the following changes to your helpers and templates:

    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>