dimanche 19 avril 2015

nodejs loops and arrays

Hey everyone i cant seem to grasp the arrays function in nodejs and am having a few problems programming it to work the way i want


what i am doing is getting json info from an api and using that for the arrays.


i would like it formatted like so



array = [ { Coin:{Balance: 0, Available: 0, Pending: 0, Exchange: 0} ]


the output from the API looks like this



[ { Currency: 'BTC',
Balance: 3.0000000000000004e-8,
Available: 3.0000000000000004e-8,
Pending: 0,
CryptoAddress: '1G29t11ioGa5cmWJKLeanbNz1TA77eS1gK' },
{ Currency: 'DGB',
Balance: 0,
Available: 0,
Pending: 0,
CryptoAddress: 'DTcrZApNRDmq41bhcPcZdhhApaLQGr7nfj' }
] }


and the script im using to try and add them to a new array is





var redis = require('redis');
client = redis.createClient()

var bittrex = require('node.bittrex.api');
bittrex.options({
'apikey': 'KEY',
'apisecret': 'SECRET',
'stream': true,
'verbose': true,
'cleartext': true,
'baseUrl': 'http://ift.tt/1MiVHVL'
});


bittrex.getbalances(function(data) {
console.log(data);
array = [];
for (var coin in data.result) {
Name = data.result[coin].Currency;
array.push(Name);

array[Name].Balance.push(data.result[coin].Balance);

array[Name].Available.push(data.result[coin].Available);
array[Name].Pending.push(data.result[coin].Pending);


client.hget("Exchange_Rates", data.result[coin].Currency, function(err, exchange) {
array[Name].Exchange.push(exchange);
});

};

console.log(array);


for (var sell in array) {
bittrex.sendCustomRequest('http://ift.tt/1Hm0ciZ' + sell + '&quantity=' + array[Name].Available + '&rate=' + array[Name].Exchange, function(data) {
console.log(data);
}, true);
};


});



This is the current error returned

/home/alastair/cryptsy.js:22 array[Name].Balance.push(data.result[coin].Balance); ^ TypeError: Cannot read property 'Balance' of undefined at /home/alastair/cryptsy.js:22:12 at /home/alastair/node_modules/http://ift.tt/1G7fS8o at Stream.write (/home/alastair/node_modules/http://ift.tt/1Hm0cj1) at Stream.stream.write (/home/alastair/node_modules/http://ift.tt/1G7fR4g) at Stream.ondata (stream.js:51:26) at Stream.EventEmitter.emit (events.js:95:17) at drain (/home/alastair/node_modules/http://ift.tt/1Hm09Uu) at Stream.stream.queue.stream.push (/home/alastair/node_modules/http://ift.tt/1G7fR4i) at Parser.parser.onToken (/home/alastair/node_modules/http://ift.tt/1Hm09Uy) at Parser.proto.write (/home/alastair/node_modules/http://ift.tt/1G7fR4k)


Aucun commentaire:

Enregistrer un commentaire