1

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!

Carol.Kar
  • 4,581
  • 36
  • 131
  • 264
  • 1
    `this.data = data;` does not assign to the global `data` variable, but to the `.data` property of whatever object you handler is invoked on - probably the `channel`. – Bergi Dec 13 '14 at 12:35
  • @Bergi Thx for your answer! Which solution for my problem do you propose? – Carol.Kar Dec 13 '14 at 12:38
  • 1
    Actually I'm not really sure what you want to do. What is `channel`, and where does it get the `message`s from? What exactly does "*I want to use the json object later*" mean - why not save it to the db immediately? It looks like your code not only suffers from the wrong assignment, but also from [the common asynchrony issue](http://stackoverflow.com/q/23667086/1048572) – Bergi Dec 13 '14 at 12:42
  • @Bergi The channel is a variable from the pusher api. I am totally ok, if I can save the data right away into the db. However, I am a newbie to js and I am totally unsure how I can add the "save to db" part to my channel method. – Carol.Kar Dec 13 '14 at 12:52
  • 1
    In that case, just move the save code (and everything else that uses `data`, e.g. the `console.log`) in the event callback. You can then also omit that global variable declaration. – Bergi Dec 14 '14 at 10:38

0 Answers0