我在win 7上运行,并已将nodeJS安装为路径变量。
我安装了nodeJs模块(server):
我正在获得我的json元素:
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
我的脚本中打印并保存数据的部分:
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);
我想引用全局变量数据,因为我想稍后使用json对象将其保存到mysql数据库中。
运行代码时,我稍后会在mysql部分中找到找不到全局对象的错误:
$ 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
任何建议我做错了什么?
感谢您的回答!