Re: Simple Question - No Answer: How to Call a Node.ACS Service from Titanium App?

201 views
Skip to first unread message
Message has been deleted

Davide Cassenti

unread,
May 20, 2013, 8:33:48 AM5/20/13
to node...@googlegroups.com
Hello,

The automatically-generated name for the exported method is not refreshPrice, but services_refreshPrice. You have to use it in order to call the method:

NodeFinanDemo.services_refreshPrice({   }, function(_e) {
    Ti.API.info(_e);
});

You can see the imported code in the file ti.cloud.NodeFinanDemo.js


In addition, in your server code you should add the content type, just before you write the response:

res.setHeader('Content-Type', 'application/json');

Hope this helps



On Sunday, May 19, 2013 3:41:16 PM UTC+2, underlabs wrote:
I’ve been trying to implement the Financial Stock Watch List Node.ACS project , with some success, though I’m blocked on how to call the service from a Titanium App, this is what I’ve done:

1- Created new Node.ACS Project ‘NodeFinanDemo’

2- Added a New Service ‘refreshPrice’ & pasted the code blow, and updated the ACS Oath & Secret key, and created a Custom Object to a user ‘stocks’ with a property called ‘symbol’ and value ‘APPL’.

var ACS = require('acs').ACS;
var logger = require('acs').logger;
var EventEmitter = require('events').EventEmitter;
var https = require('http');
var sessionID = null;
var stocks;
ACS.init('*****', '*****'); // used my ACS OAuth key and secret

function refreshPrice(req, res) {
    var uid = req.query.uid;
    var Event = new EventEmitter;
    var results = [],
        evt_count = 0;
    // SETUP EVENT LISTENER
    Event.on("HTTP_COMPLETE", function (e) {
        console.log("EVENT: HTTP_COMPLETE");
        evt_count++;
        console.log(evt_count);
        if (evt_count >= stocks.length) {
            console.log("close response, results = " + JSON.stringify(results));
            res.write(JSON.stringify(results));
            res.end();
        }
    });
    ACS.Objects.query({
        page: 1,
        per_page: 100,
        classname: "stocks",
        where: {
            user_id: uid,
        }
    }, function (data) {
        console.log(JSON.stringify(data));
        stocks = data.stocks;
        var l = stocks.length;
        if (l == 0) {
            Event.emit("HTTP_COMPLETE");
        };
        for (var i = 0; i < l; i++) {
            marketOnDemandQuote(stocks[i].symbol);
        }
    })

    function marketOnDemandQuote(symbol) {
        var myreq =
            https.get('http://dev.markitondemand.com/Api/Quote/json?symbol=' + symbol, function (r) {
            r.setEncoding('utf8');
            r.on('data', function (chunk) {
                console.log(chunk);
                var o = {}, d = JSON.parse(chunk);
                o.symbol = d.Data.Symbol;
                o.name = d.Data.Name;
                o.lastPrice = d.Data.LastPrice;
                o.change = d.Data.Change;
                o.changePercent = d.Data.ChangePercent;
                o.timestamp = d.Data.Timestamp;
                results.push(o);
                Event.emit("HTTP_COMPLETE");
            });
        });
    }
};
exports.refreshPrice = refreshPrice;
// Source code attribution: Asim Siddiqui and Bert Grantges


3- Published App & got a full url:https://<appid>.cloudapp.appcelerator.com

4- Created a New Mobile App ‘NodeACSAppConnection’

5- Imported the Node Project into a FirstView.js in Titanium

6- I see the code that got added to FirstView.js which now looks like this:

// @autogenerated:start:ti.cloud.NodeFinanDemo

var NodeFinanDemo = require("ti.cloud.NodeFinanDemo");

// @autogenerated:end:ti.cloud.NodeFinanDemo

var Cloud = require('ti.cloud');

function FirstView() {

var self = Ti.UI.createView();

Cloud.Users.login({

    login: 'default',

    password: 'imemine'

    }, function (e) {

        if (e.success) {

            var user = e.users[0];

            NodeFinanDemo.refreshPrice({    }, function (_e) {

                Ti.API.info(_e);

            });

        } else {

            alert('Error:\n' +

                ((e.error && e.message) || JSON.stringify(e)));

        }

    });

return self;

}

module.exports = FirstView;

7- No idea how to call the refreshPrice from the Ti App?

Note: I breakpointed the line `NodeFinanDemo.refreshPrice` and peaked into NodeFinanDemo:
URL: “http://localhost:53945″ // even though I can access that locally, shouldn’t this be https://<appid>.cloudapp…com ?
uri: “app://ti.cloud.NodeFinanDemo.js”
id: “ti.cloud.NodeFinanDemo”

Also if I run the code above, I get a runtime error:
message = “‘undefined’ is not a function (evaluating ‘NodeFinanDemo.refreshPrice({ }, function (_e) {\n Ti.API.info(_e);\n })’)”;

underlabs

unread,
May 20, 2013, 9:32:22 AM5/20/13
to node...@googlegroups.com
Excellent Davide,

I think I got the hang of it... I did not notice the new file added :)
Thanks a lot!!

-------

    password: '******'

    }, function (e) {

        if (e.success) {

            var user = e.users[0];

            NodeFinanDemo.refreshPrice({    }, function (_e) {

                Ti.API.info(_e);

            });

        } else {

            alert('Error:\n' +

                ((e.error && e.message) || JSON.stringify(e)));

        }

    });

return self;

}

module.exports = FirstView;

7- No idea how to call the refreshPrice from the Ti App?

Note: I breakpointed the line `NodeFinanDemo.refreshPrice` and peaked into NodeFinanDemo:
URL: “http://localhost:53945″ // even though I can access that locally, shouldn’t this be https://<appid>.cloudapp…com ?
uri: “app://ti.cloud.NodeFinanDemo.js”
id: “ti.cloud.NodeFinanDemo”

Also if I run the code above, I get a runtime error:
message = “‘undefined’ is not a function (evaluating ‘NodeFinanDemo.refreshPrice({ }, function (_e) {\n Ti.API.info(_e);\n })’)”;


Message has been deleted

underlabs

unread,
May 20, 2013, 9:56:52 AM5/20/13
to node...@googlegroups.com
Sorry to bother you again :)

The Initial services_refreshPrice returned nothing for me.

So I created a new service: services_simpleReturn:

1- In the Node.ACS App, I added to the controllers/services.js :

function simpleReturn(req, res) {
    res.setHeader('Content-Type''application/json');  // Added for Mobile Apps
    res.send('Hello, world!');
};
exports.simpleReturn simpleReturn;

2- Deployed the App (overwrite)
3- Re-Imported Node.ACS Binding (NodeFinanDemo into FirstView.js), looks like:

function simplecall(){
     NodeFinanDemo.services_simpleReturn({}, callback);
     function callback(_e) {
          Ti.API.info(_e);
     };
}

4- Traces out: [INFO] Cannot GET /simpleReturn

I'm probably missing something?

underlabs

unread,
May 20, 2013, 10:15:03 AM5/20/13
to node...@googlegroups.com
SOLVED!!

Thanks again Davide

Davide Cassenti

unread,
May 20, 2013, 10:20:17 AM5/20/13
to node...@googlegroups.com
Thanks,

great news. Let me know if you need anything else!
Reply all
Reply to author
Forward
0 new messages