Sunday, 7 May 2017

Exporting is Confusing Me

Am required to use Node for a project, and I'm running into a behavior I can't explain or resolve. I'm hoping someone here can help me.What's happening during the Accounts_callback doesn't seem to be working properly. It should be setting this.accounts_list to the data returned to the callback. But it does not seem to.This is all the more strange because the OTHER uses of the module, the callback itself and the creation of xcAccounts, seem to have no problem.Here's the code in the module: var _ = require('underscore'); var moment = require('moment'); var sysMsg = require('./system_messages'); //////////////////////////////////////////////////////////////////////////////////////// /////// ACCOUNTS CALLBACK ////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////// function Accounts(client) { this.authedClient = client; this.accounts_list = []; } Accounts.prototype.count = function() { return this.accounts_list.length; } Accounts.prototype.Accounts_callback = function( err, response, data ) { var line = ''; var dash = '-'; if(data) { if( ! _.isEqual(data,this.accounts_list) ) { // loop thru data and get 'currency' and 'balance' line = ''; var arrayLength = data.length; for (var i = 0; i < arrayLength; i++) { if(i>0) { line+=' '; } line += data[i].currency + ':' + (data[i].available * 1).toFixed(8); } accounts_ticker = moment().format('YYYY.MM.DD@HH:MM') + ' | ' + line + ' '; var header = '+----Your Accounts'; header += dash.repeat(accounts_ticker.length-header.length+1) + '|'; console.log(); console.log(header) console.log('|' + accounts_ticker + '|'); console.log('+' + dash.repeat(accounts_ticker.length) + '+') this.accounts_list = data; } } }; module.exports = Accounts; I'm trying to call it like this from the main file:var xcAccounts = new Accounts(authedClient); // SEEMS TO WORK if(xcAccounts.count() > 0)... // DOES NOT SEEM TO WORK (count is never more than 0) function updateAccounts() { authedClient.getAccounts(xcAccounts.Accounts_callback); // THIS WORKS... } var KeepAccountsUpdated = setInterval(updateAccounts, 200); // THIS WORKS TO TRIGGER THE CALLBACK Anybody see what I'm doing wrong, or what I can try to resolve this?

Submitted May 08, 2017 at 02:33AM by cklester

No comments:

Post a Comment