Rob Fyffe
Rob Fyffe

Reputation: 729

Passing data from service to controller

I have created a service with the purpose of accessing an API. I need to return data to my controller but am unsure how to do this as I completely new to sails.

My Service:

// API call to get the ID of contact within Get Response with email address
getContact: function(options) {

    // establish connection to API
    var api = new getResponse(apiKey, apiUrl);

    var contact = api.getContactsByEmail(options.email, null, null, function (response) {
        JSON.stringify(response);
        console.log(JSON.stringify(response));
    });

    return contact;

},

I know the API call is working as when I log the response I get the correct response:

{"success":true,"data":{"error":null,"id":1,"result":{"sds":{"ip":null,"name":"Full Name","origin":"api","cycle_day":0,"email":"[email protected]","campaign":"id","created_on":"date","changed_on":null}}}}

My Controller:

index: function(req, res) {

    var contact = GetresponseService.getContact({email: '[email protected]'});

    console.log(contact);
    return res.send(contact);

}

I want to retrieve the ID value but when I log the value of contact I get undefined. I think my problem is related to the scope but not sure.

Can anyone point me in the right direction?

Upvotes: 0

Views: 604

Answers (2)

Maxali
Maxali

Reputation: 1962

Because you are directly assigning a value from api.getContactsByEmail() which does not return a value.

By the nature of node.js, the function api.getContactsByEmail() gives you callback with the response. You have to get the value from within the anonymous callback function like this:

// API call to get the ID of contact within Get Response with email address
getContact: function(options) {

    // establish connection to API
    var api = new getResponse(apiKey, apiUrl);
    var contact =  "";
    api.getContactsByEmail(options.email, null, null, function (response) {

        contact = response;

        JSON.stringify(response);
        console.log(JSON.stringify(response));

        return contact; 
    });

}

more ...

In my opinion, its better to return a callback instead of direct return of value.

// API call to get the ID of contact within Get Response with email address
getContact: function(options, callback) {

    // establish connection to API
    var api = new getResponse(apiKey, apiUrl);
    var contact =  "";    
    api.getContactsByEmail(options.email, null, null, function (response) {

        contact = response;

        JSON.stringify(response);
        console.log(JSON.stringify(response));

        if(typeof(callback) == "function")
            callback(contact);
        else
            return contact; // return contact if there is callback func.
    });

}

You can use it like:

index: function(req, res) {

    var contact;
    GetresponseService.getContact({email: '[email protected]'}, function(contactResult) {

        contact = contactResult;
        console.log(contact);
        return res.send(contact);
    });


}

Upvotes: 1

Ian Kidd
Ian Kidd

Reputation: 66

Everything looks like it should work, however I think you're running into an issue with this piece

var contact = api.getContactsByEmail(options.email, null, null, function (response) {
        JSON.stringify(response);
        console.log(JSON.stringify(response));
    });

api.getContactsByEmail is asynchronous I assume, so this declarative statement won't work.

Instead declare contact and return it inside the callback, something like:

api.getContactsByEmail(options.email, null, null, function (response) {
        JSON.stringify(response);
        console.log(JSON.stringify(response));
        var contact = response.contacts; //or however you access the contact(s) from the response variable
//You should also be watching for errors in your callbacks, but that's a different topic

        return contact;
    });

Read up on asynchronous calls in javascript and make sure you have a solid grasp on when data is accessible when using them.

Upvotes: 0

Related Questions