I am running on win 7 and have installed nodeJS as a path variable.
I have installed a nodeJs module(server):
I am getting my json elements like that:
json:
{ channel: 'ticker.160',
trade:
{ timestamp: 1418473296,
datetime: '2014-12-13 07:21:36 EST',
marketid: '160',
topsell: { price: '0.00007723', quantity: '1.48685240' },
topbuy: { price: '0.00007714', quantity: '38.89032927' } } }
Price: 0.00007714 Quantity: 38.89032927 Timestamp: 1418473296
The part of my script which prints and saves the data:
var data;
channel.bind("message", function(data) {
console.log(data);
this.data = data;
console.log("Price: ", data.trade.topbuy.price,
"Quantity:", data.trade.topbuy.quantity,
"Timestamp:", data.trade.timestamp);
});
console.log(data);
/**
* save data to db
*/
var trade = {exchange : 'crispy.com',
market_id : 'ticker.160',
typeOfTransaction : 'buy',
price : data.trade.topbuy.price,
quantity : data.trade.topbuy.quantity,
created_at : data.trade.timestamp,
updated_at : data.trade.timestamp
};
var query = connection.query('INSERT INTO trades SET ?', trade, function(err, result) {
});
console.log(query.sql);
I want to reference to the global variable data, because I want to use the json object later to save it into a mysql db.
When running the code I get later in my mysql part an error that the global object is not found:
$ node server.js
undefined
c:\xampp\htdocs\project\psher\node_modules\server.js:52
price : data.trade.topbuy.price,
^
TypeError: Cannot read property 'trade' of undefined
at Object.<anonymous> (c:\xampp\htdocs\project\psher\node_mo
dules\server.js:52:18)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
Any recommendations what I am doing wrong?
I appreciate your answer!